NOIP 2014解方程式数論+シミュレーション
15718 ワード
多項式方程式の[1,m][1,m][1,m][1,m]の整数解を求めます.标题:思いもよらなかったでしょう、秦九韶アルゴリズムは多項式の計算回数を減らして暴力列挙[1,m][1,m][1,m][1,m][1,m][1,m]それでいいの?いいえ、係数が大きすぎるので、型を取ります.原数をそれぞれ複数の素数に模し,答えが0であれば近似的に答えと見なすことができる.これなら洛谷でACができますが、BZOJでTLEができます.どうして?洛谷データ水+評価機が速いため、これ以上考えていない人が多い.真・満点のやり方:モジュールpの意味でf(x)=0 f(x)=0 f(x)=0であればf(x+k∗p)=0 f(x+k*p)=0 f(x+k∗p)=0であることに気づくので,素数範囲まで列挙するだけでよい.
#include
#include
const int MOD[3] = {20029,22277,23333};
const int MaxMod = 3;
int n, m;
char ch[20001];
long long a[5][105];
int Mod[5][40001];
int ans[1000001];
inline void read(int i){
int f = 1; char ch = getchar();
while(ch < '0' || ch > '9'){if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9'){
for(int t = 0; t < MaxMod; t++)
a[t][i] = (a[t][i] * 10 + ch - '0') % MOD[t];
ch = getchar();
}
if(f == -1) for(int t = 0; t < MaxMod; t++)
a[t][i] = MOD[t] - a[t][i];
}
inline bool pd(int x, int t){
long long sum = a[t][n];
for(int i = n - 1; i >= 0; i--)
sum = (sum * x + a[t][i]) % MOD[t];
return sum == 0;
}
inline bool check(int x){
for(int t = 0; t < MaxMod; t++)
if(!Mod[t][x % MOD[t]]) return false;
return true;
}
int main(){
scanf("%d%d", &n, &m);
for(int i = 0; i <= n;i++) read(i);
for(int t = 0; t < MaxMod; t++)// MOD
for(int x = 1; x < MOD[t]; x++)// x
if(pd(x, t)) Mod[t][x] = true;
for(int x = 1; x <= m; x++)
if(check(x)) ans[++ans[0]] = x;
printf("%d
", ans[0]);
for(int i = 1; i <= ans[0]; i++)
printf("%d
", ans[i]);
return 0;
}