【luogu2312】【noip2014】 解方程 [数学 同余]

P2312 解方程

根据同余的性质 可以知道如果当前解x成立的话 则在式子左右同时模一个数该式还会成立 所以可以在输入的时候进行取模 然后挨个枚举 m是在106

然后在累乘pai开一下long long 可能会爆int(别问我为什么知道) 

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<cstring>
 5 #include<cmath>
 6 #include<stack>
 7 #include<algorithm>
 8 using namespace std;
 9 #define ll long long
10 #define rg register
11 const int N=2000+5,M=1e8+7;
12 int n,m,cnt=0,a[N],ans[N];
13 template <class t>void rd(t &x)
14 {
15     x=0;int w=0;char ch=0;
16     while(!isdigit(ch)) w|=ch=='-',ch=getchar();
17     while(isdigit(ch)) 
18     x=(((x<<1)+(x<<3))%M+(ch^48))%M,ch=getchar();
19     x=w?-x:x;
20 }
21 
22 int main()
23 {
24     //freopen("in.txt","r",stdin);
25     //freopen("nocows.out","w",stdout);
26     rd(n),rd(m);
27     for(rg int i=0;i<=n;++i) rd(a[i]);
28     for(rg int x=1;x<=m;++x)
29     {
30         ll pai=1,sum=a[0];
31         for(rg int i=1;i<=n;++i)
32         {
33             pai=(pai*x)%M;
34             sum=(sum+(pai*a[i])%M)%M;
35         }
36         if(!sum) ans[++cnt]=x;
37     }
38     printf("%d
",cnt);
39     for(rg int i=1;i<=cnt;++i) printf("%d
",ans[i]);
40     return 0;
41 }
原文地址:https://www.cnblogs.com/lxyyyy/p/10881971.html