1.题目描述:点击打开链接
2.解题思路:本题要求找到合适的一组a,b,使得按照递推公式能输出正确的x2,x4...可以枚举a值,通过列写方程得到b的值,但这里有一个问题,这里是一个同余方程,等号的一端带有k*10001,这时就应该迅速的想到利用扩展gcd来解决,已知量为1001,a+1,求出gcd(10001,a+1)以及线性方程的系数x,y即可。当发现计算出的数和原来的输入矛盾时,说明a是非法的。由于a介于0~10000之间,因此即使枚举所有的a,效率也足够高。
3.代码:
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;
#define maxn 10001
typedef long long LL;
int x[maxn];
LL a, b;
void gcd(LL a, LL b, LL&d, LL&x, LL&y)
{
if (b == 0){ d = a, x = 1, y = 0; }
else
{
gcd(b, a%b, d, y, x);
y -= x*(a / b);
}
}
int main()
{
//freopen("test.txt", "r", stdin);
int T;
cin >> T;
for (int i = 1; i <= 2 * T - 1; i += 2)
cin >> x[i];
for (a = 0; a < maxn; a++)
{
LL tmp = (LL)x[3] - a*a*x[1];
LL d, xx;
gcd(maxn, a + 1, d, xx, b);
if (tmp % d != 0) continue;
b = b*(tmp/d);
int y[maxn];
int ok = 1;
memset(y, 0, sizeof(y));
y[1] = x[1];
for (int i = 2; i <= 2 * T; i++)
{
y[i] = (a*y[i - 1] + b) % maxn;
if (x[i]>0)
if (x[i] != y[i]){ ok = 0; break; }
}
if (!ok)continue;
else
{
memcpy(x, y, sizeof(y));
for (int i = 2; i <= 2 * T; i += 2)
cout << x[i] << endl;
break;
}
}
return 0;
}