洛谷P2312解方程

传送门

思路分析

怎么求解呢?

其实我们可以把左边的式子当成一个算式来计算,从1到 $ m $ 枚举,只要结果是0,那么当前枚举到的值就是这个等式的解了。可以通过编写一个 $ bool $ 函数来判断算式的值是不是0

至于如何计算这个多项式,用秦九韶算法就可以解决

细节提示 :

1.防爆 $ int $ 常用方法:模大质数!(另:好像模一个质数有的时候会出事233可以多模几个大质数

2.最好用上读入优化,而且边读边取模。

3 . $ sum $ 每次都要清零

#include <iostream> 
#include <cstdio>
#include <cstring>
#include <algorithm>
#define re register
using namespace std;
const long long mod = 1000000007;

inline int read(){
	char ch = getchar();
	long long f = 1 , x = 0 ;
	while(ch > '9' || ch < '0') {if(ch == '-') f = -1;ch = getchar();}
	while(ch >= '0' && ch <= '9'){x = ((x << 1) + (x << 3) + ch - '0') % mod ;ch = getchar();}
	return x * f;
}

long long n,m,ans,cnt,sum;
bool flag = true;//用来判断是否有解 
long long a[110],key[1000005];

inline bool calc(long long x) {
	sum = 0 ;
	for(re long long i = n ; i >= 1 ; --i) {
		sum = ((a[i] + sum) * x) % mod;
	}
	sum = (sum + a[0]) % mod;
	return !sum;
}

int main(){
	n = read();  m = read();
	for(re long long i = 0 ; i <= n ; ++i) {
		a[i] = read();
	}
	for(re long long i = 1 ; i <= m ; ++i) {
		if(calc(i)){
			flag = false;
			ans++;
			key[++cnt] = i ;
		}
	}
	if(flag) {
		printf("%lld
",ans);
		return 0;
	}
	printf("%lld
",ans);
	for(re long long i = 1 ; i <= cnt ; ++i)
		printf("%lld
" , key[i]);
	return 0;
}
原文地址:https://www.cnblogs.com/Stephen-F/p/9930791.html