x2 = (a * x1 + b) % 10001;
x3 = (a * x2 + b) % 10001;
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是我们枚举的算已知量 x1 x3 已知
所以把x3 - a * a * x1看作c
b看作x -k看作y
通过扩展欧几里得算法求出b 求出来的-k是没作用的 就放着
然后由递推式求出答案序列
扩展欧几里得的算法详解我博客里有哦
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=1e4+10;
const int mod=10001;
ll x[maxn];
ll exgcd(ll a,ll b,ll &x,ll &y)
{
ll ans,t;
if(b==0) //gcd border
{
x=1;
y=0;
return a;// return final gcd
}
ans=exgcd(b,a%b,x,y);// this value via the final gcd
t=x;
x=y;
y=t-(a/b)*y;
return ans;
}
int main()
{
int t;
scanf("%d",&t);
for(int i=1;i<2*t;i+=2)
{
scanf("%lld",&x[i]);
}
//for(int i=1;i<=2*t;i++)
//cout<<x[i]<<' ';
//cout<<endl;
for(ll a=0;;a++)
{
ll c=x[3]-a*a*x[1];
ll b,y;
ll gc=exgcd(a+1,mod,b,y);//y = -k
//cout<<t<<" "<<c<<endl;
if(c%gc) continue;
b=b*c/gc;
bool flag=1;
for(int i=2;i<=2*t;i++)
{
if(i&1)
{
if(x[i] != ((a*x[i-1]+b)%mod))
{
flag=0;
break;
}
}
else
{
x[i]=(a*x[i-1]+b)%mod;
}
}
if(flag==1) break;
}
for(int i=2;i<=2*t;i+=2)
{
printf("%lld\n",x[i]);
}
return 0;
}