Uva 12169 Disgruntled Judge(枚举)

链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=326&problem=2290&mosmsg=Submission+received+with+ID+2196098

大意:丧心病狂的出题人设计了一个长度为2t的数列x,满足x[i+1]=a*x[i]+b mod 10001,将x[1],x[3]....作为输入,x[2],x[4]....作为输出,其中a  b和x[i]均在[0,10001]中,给出输入,求输出.

分析:首先10001=73*137,可以枚举a,相应地算出b,b满足x[3]-a*a*x[1]=(a+1)*b mod 10001,用中国剩余定理算一下b,然后就可以推出所有的x[i],检查下是否与输入矛盾,不矛盾的话就是一组解。

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 typedef long long ll;
 5 int x[2*100+5];
 6 int inv(int a,int p){
 7     for(int i=0;i<p;i++){
 8         if((a*i)%p==1)return i;
 9     }
10 }
11 int main(){
12     int t;
13     cin>>t;
14     memset(x,-1,sizeof(x));
15     for(int i=0;i<t;i++){
16         cin>>x[2*i+1];
17     }
18     for(int a=0;a<10001;a++){
19         bool ok=true;
20         int b=(1096*((x[3]-(ll)a*a*x[1])%10001)*inv(a+1,73))%10001+(8906*(((x[3]-(ll)a*a*x[1])%10001)*inv(a+1,137)))%10001;
21         b%=10001;
22         for(int i=2;i<=2*t;i++){
23             int q=(a*x[i-1]+b)%10001;
24             if(i%2&&x[i]!=q){
25                 ok=false;break;
26             }
27             x[i]=q;
28         }
29         if(ok){
30             for(int i=2;i<=2*t;i+=2)
31                 cout<<x[i]<<endl;
32         }
33     }
34     return 0;
35 }
原文地址:https://www.cnblogs.com/7391-KID/p/7081377.html