这个题在求a,b的时候。 需要两个等式。 写出 x3 x2 的关系式 和 x2 x1的关系式。 联立之后。
便可以写成 (a + 1)b + 10001 y = x3 - a*a*x1;
这个式子 明显的就是 在模拟 a的情况下。 a+1 10001为系数 已知,在知道 c = x3 - a*a*x1 则求 b,y。
在刘汝佳第二版。P314最上面 写的很清楚。 ax+by=c 的一组解是 (x0c/g , y0c/g)。 其中 x0 y0 g 都可以 在扩展欧几里得里面求出。
在这一组解中 求出的 x y是任意一组。 所以计算就好。
如果想把 b 求导最小。 在 P313 提示10-1 中 写的很明白了。 求出 b‘ = b/d; b(min) = x0 + b’ (原式为 x0+kb’ ,为了取最小 令k = 1)
在求出 a,b之后 进行求解数列, 注意 如果得出的某一奇数项 与给出的不相同。 那么 a,b不成立, 需要重新枚举a。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <string>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <cctype>
using namespace std;
#define ll long long
typedef unsigned long long ull;
#define maxn 1000+10
#define INF 1<<30
ll 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 n;
scanf("%d",&n);
ll s[maxn] = {0};
for(int i = 1; i <= n; i++){
scanf("%lld",&s[2*i - 1]);
}
for(int i = 0;i <= 10000; i++){
int flag = 1;
ll a = i;
ll b,k,d;
gcd(a+1,10001,d,b,k);
ll c = (s[3] - s[1]*a*a)%10001;
if(c % d) continue; // 无整数解
b = b * c / d % 10001; // 求出一组解
ll x[maxn];
memcpy(x,s,sizeof(s));
for(int j = 1; j < 2 * n; j++){
int xj = (a * x[j] + b)%10001;
if(j % 2 == 1)
x[j+1] = xj;
else if(xj != x[j+1]){
flag = 0;
break;
}
}
if(flag){
for(int j = 2; j <= 2*n ; j+=2){
printf("%d\n",x[j]);
}
break;
}
}
return 0;
}