发布于 

【NOIP2014】解方程 题解

NOIP2014

核心思路

说实话刚拿到这道题时,除了30pts,没有任何思路。

设原多项式方程的对应函数为:

f(x)=a0+a1x++anxnf(x)=a_0+a_1x+\cdots+a_nx^n

题目所求就是 f(x)=0f(x)=0x[1,m]x\in [1,m] 的整数解。

考虑 f(x)modpf(x) \bmod p ,当 f(x)=0f(x)=0 时显然等于00

那么直接 Θ(nm)\Theta(nm) 暴力枚举 [1,m][1,m] 并计算答案 f(x)modpf(x) \bmod p 是否为00

选择一个好的模数很重要。

读入的时候用快读,边读边模。

完整代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>

using namespace std;
#define int ll
#define REP(i,e,s) for(register int i=e; i<=s; i++)
#define DREP(i,e,s) for(register int i=e; i>=s; i--)
#define ll long long
#define DE(...) fprintf(stderr,__VA_ARGS__)
#define DEBUG(a) DE("DEBUG: %d\n",a)
#define file(a) freopen(a".in","r",stdin);freopen(a".out","w",stdout)

const int MAXN=100000+10,MOD=19260817;

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

int a[MAXN],n,m;

int f(int x) {
int ans=0,prod=1;
REP(i,0,n) {
ans=(ans+prod*a[i])%MOD;
prod=(prod*x)%MOD;
}
return ans;
}

int book[MAXN];

int ans[MAXN],cnt;

signed main() {
n=read(),m=read();

REP(i,0,n) a[i]=read();

REP(i,1,m) if(f(i)==0) ans[++cnt]=i;
printf("%lld\n",cnt);
REP(i,1,cnt) printf("%lld\n",ans[i]);
return 0;
}