时空限制 1000ms / 128MB
题目描述
已知多项式方程:
a
0
+
a
1
x
+
a
2
x
2
+
⋯
+
a
n
x
n
=
0
a_0+a_1x+a_2x^2+\cdots+a_nx^n=0
a0+a1x+a2x2+⋯+anxn=0
求这个方程在 [1,m]内的整数解(n 和 m 均为正整数)。
输入格式:
共 n + 2行。
第一行包含 2 个整数 n, m每两个整数之间用一个空格隔开。
接下来的 n+1 行每行包含一个整数,依次为
a
0
,
a
1
,
a
2
…
a
n
a_0,a_1,a_2\ldots a_n
a0,a1,a2…an
输出格式:
第一行输出方程在 [1,m]内的整数解的个数。
接下来每行一个整数,按照从小到大的顺序依次输出方程在 [1,m]内的一个整数解。
说明
对于 30% 的数据:
0
<
n
≤
2
,
∣
a
i
∣
≤
100
,
a
n
≠
0
,
m
<
100
0<n\le 2,|a_i|\le 100,a_n≠0,m<100
0<n≤2,∣ai∣≤100,an̸=0,m<100
对于 50% 的数据:
0
<
n
≤
100
,
∣
a
i
∣
≤
1
0
100
,
a
n
≠
0
,
m
<
100
0<n\le 100,|a_i|\le 10^{100},a_n≠0,m<100
0<n≤100,∣ai∣≤10100,an̸=0,m<100
对于 70% 的数据:
0
<
n
≤
100
,
∣
a
i
∣
≤
1
0
10000
,
a
n
≠
0
,
m
<
1
0
4
0<n\le 100,|a_i|\le 10^{10000},a_n≠0,m<10^4
0<n≤100,∣ai∣≤1010000,an̸=0,m<104
对于 100% 的数据:
0
<
n
≤
100
,
∣
a
i
∣
≤
1
0
10000
,
a
n
≠
0
,
m
<
1
0
6
0<n\le 100,|a_i|\le 10^{10000},a_n≠0,m<10^6
0<n≤100,∣ai∣≤1010000,an̸=0,m<106
题目分析
很明显取膜hash
即将上述多项式在运算过程中对大质数取膜,最后判断是否为0
运算过程用秦九韶公式搞搞就行
但是注意到直接这样计算复杂度是
O
(
n
∗
m
)
O(n*m)
O(n∗m)的,明显会被卡啊
考虑取膜p的质数先取一个稍小的
若在膜p意义下
F
(
x
)
!
=
0
F(x)!=0
F(x)!=0,则一定有
F
(
x
+
p
)
!
=
0
F(x+p)!=0
F(x+p)!=0成立
具体复杂度不太会证,但感性分析会降低很多
当然取膜hash并不保证其正确性
在对较小质数取膜意义下有
F
(
x
)
=
0
F(x)=0
F(x)=0,再带入一个大质数重新验证此解
#include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstring>
#include<cstdio>
#include<bitset>
using namespace std;
typedef long long lt;
lt mod1=10007,mod2=1e9+7;
const int maxn=1000010;
int n,m,cnt;
int judge[maxn*20];
lt a[maxn],b[maxn],ans[maxn];
void read(int i)
{
lt f=1,x1=0,x2=0;
char ss=getchar();
while(ss<'0'||ss>'9'){if(ss=='-')f=-1;ss=getchar();}
while(ss>='0'&&ss<='9'){
x1=((x1<<1)+(x1<<3)+ss-'0')%mod1;
x2=((x2<<1)+(x2<<3)+ss-'0')%mod2;
ss=getchar();
}
a[i]=f*x1; b[i]=f*x2;
}
int check(lt x,int d)
{
lt res=0;
if(d==0)
{
for(int i=n;i>=1;--i)
res=((res+a[i])*x)%mod1;
res=(res+a[0])%mod1;
}
else if(d==1)
{
for(int i=n;i>=1;--i)
res=((res+b[i])*x)%mod2;
res=(res+b[0])%mod2;
}
return res==0;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<=n;++i) read(i);
for(int i=1;i<=m;++i)
{
if(judge[i]) continue;
if(!check(i,0))
{
if(i+mod1>20000001) judge[(i+mod1)%mod1]=1;
else judge[i+mod1]=1;
}
else if(check(i,1)) ans[++cnt]=i;
}
printf("%d\n",cnt);
for(int i=1;i<=cnt;++i)
printf("%d\n",ans[i]);
return 0;
}