给出一个数列:x2=(a*x1+b)%10001;的奇数项,求出偶数项。
题目:UVA 12169
分析:
x2 = (a * x1 + b) % 10001;
x3 = (a * x2 + b) % 10001;
联立2个式子
x3 = (a * (a * x1 + b) % 10001 + b ) % 10001;
x3 = (a * (a * x1 + b) + b) % 10001;
所以 x3 + 10001 * k = a * a * x1 + (a + 1) * b;
x3 - a * a * x1 = (a + 1) * b + 10001 * (-k);
枚举a,即拓欧几里得就可以求出 b。然后就可以求出整个数列,匹配是否正确即可。
代码:
#include<stdio.h> #include<iostream> #include<algorithm> using namespace std; const int max_=105*2; typedef unsigned long long ll; ll f[max_]; ll g,k,B; void gcd_(ll a,ll b,ll &d,ll &x,ll &y) { if(!b) { d=a; x=1; y=0; } else { gcd_(b,a%b,d,y,x); y-=x*(a/b); } } int main() { int t; cin>>t; for(int i=1;i<2*t;i+=2) { cin>>f[i]; } //ll c=f[1] for(int a=0;a<10001;a++) { bool falg=0; ll c=f[3]-(a*a*f[1]); gcd_(a+1,10001,g,B,k); if(c%g) continue; else B=B*(c/g); for(int i=2;i<=2*t;i++) { ll temp=(a*f[i-1]+B)%10001; if(i%2) { if(temp!=f[i]){ falg=1;break; } } else f[i]=temp; } if(!falg) break; } for(int i=2;i<=2*t;i+=2) cout<<f[i]<<endl; }