http://blog.youkuaiyun.com/u013451221/article/details/38497029
感谢原博主提供的思想。
带扩展二字的算法都可以出奇迹啊!
#include<iostream>
using namespace std;
typedef unsigned long long LL;
LL num[300];
void Gcd(LL a,LL b,LL &gcd,LL &x,LL &y)
{
if(!b) { gcd =a ; x= 1; y = 0;}
else {
Gcd(b,a%b,gcd,y,x); y -= x*(a/b);
}
}
int main() {
int T;
while(cin >> T)
{
for(int i = 1; i < T*2; i+=2)
cin >> num[i];
for(int a = 0; a <= 20000; ++a)
{
LL temp = num[3] - a*a*num[1]; //推导式子: num2 = (num1 * a + b) %10001;
LL gcd, x, y; // num3 = (num2 * a + b) % 10001;
Gcd(10001, a+1, gcd, y, x); // num3 - a*a*num1 = (a+1) * b + 10001 * k;
if(temp % gcd) continue; // 思路就是枚举a,求满足式子b;在推导num数组,看是否符合。
x = x * temp / gcd; // 解为(x*t/gcd,y*t/gcd) 一组解
bool jug = true;
for(int i = 2; i <= T *2; ++i)
{
if(i & 1) {
if(num[i] != (num[i-1] * a + x) % 10001)
{ jug = false; break;}
}
else num[i] = (num[i-1] * a + x) % 10001;
}
if(jug) break;
}
for(int i = 2; i <= T*2; i+=2)
cout << num[i] << endl;
}
return 0;
}