NOIp 2014解方程

乍看一眼以为是高精,随便写了个usigned long long交上去拿了50分,翻了翻题解才知道用了我没用过的算法——秦九韶算法,算是道模板了吧,不过这道题中的对10^10000的数的处理值得记住,并不是告诉了模一个质数才是模质数的题,凡是位数特别多且不要求求具体答案的大概都可能用到模质数吧

代码

 1 #include
 2 #include
 3 
 4 using namespace std;
 5 
 6 typedef long long ll;
 7 
 8 const int mod = 1e9+7;
 9 
10 ll read(){
11     ll ans = 0;char ch = getchar(),last = ch;
12     while(ch < '0'||ch > '9')last = ch,ch = getchar();
13     while('0' <= ch&&ch <= '9')ans = (ans*10+ch-'0')%mod,ch = getchar();
14     if(last == '-')return -ans;return ans;
15 }
16 
17 int a[110],sta[110];
18 int n,m,top = 0;
19 
20 bool check(int x){
21     ll tot = 0;
22     for(int i = n;i >= 0;i--)tot = (tot*x + a[i])%mod;
23     return !tot;
24 }
25 
26 int main(){
27     n = read(),m = read();
28     for(int i = 0;i <= n;i++)a[i] = read();
29     for(int i = 1;i <= m;i++)if(check(i))sta[++top] = i;
30     printf("%d\n",top);
31     for(int i = 1;i <= top;i++)printf("%d\n",sta[i]);    
32 return 0;
33 }

 

转载于:https://www.cnblogs.com/Wangsheng5/p/11530956.html

你可能感兴趣的:(c/c++,数据结构与算法)