题目大意:求一个多项式方程在
[
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<cstdio>
#include<cstring>
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\n", ans[0]);
for(int i = 1; i <= ans[0]; i++)
printf("%d\n", ans[i]);
return 0;
}