UVA12169 Disgruntled Judge
枚举a, 求出b,然后代入看a和b是否是对的。
具体方法:通过x1和x3可以求出b :
x2 = (a * x1 + b) % mod (1)
x3 = (a * x2 + b) % mod (2)
把(2)代入(1)得
x3 = (a2 * x1 + a * b + b) % mod
整理得
x3 + k * mod = a2 * x1 + b * (a + 1)
于是得到了一个只有两个参数的不定方程,用exgcd求解即可。
1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #include<cmath> 5 #include<cstring> 6 #include<cstdlib> 7 #include<cctype> 8 #include<stack> 9 #include<queue> 10 #include<vector> 11 using namespace std; 12 #define enter puts("") 13 #define space putchar(' ') 14 #define Mem(a, x) memset(a, x, sizeof(a)) 15 #define rg register 16 typedef long long ll; 17 typedef double db; 18 const int INF = 0x3f3f3f3f; 19 const db eps = 1e-8; 20 const int maxn = 1e5 + 5; 21 const int mod = 10001; 22 inline ll read() 23 { 24 ll ans = 0; 25 char ch = getchar(), las = ' '; 26 while(!isdigit(ch)) las = ch, ch = getchar(); 27 while(isdigit(ch)) ans = ans * 10 + ch - '0', ch = getchar(); 28 if(las == '-') ans = -ans; 29 return ans; 30 } 31 inline void write(ll x) 32 { 33 if(x < 0) putchar('-'), x = -x; 34 if(x >= 10) write(x / 10); 35 putchar(x % 10 + '0'); 36 } 37 38 int n; 39 ll f[maxn], g[maxn]; 40 41 void exgcd(ll a, ll b, ll &x, ll &y, ll &d) 42 { 43 if(!b) d = a, x = 1, y = 0; 44 else exgcd(b, a % b, y, x, d), y -= a / b * x; 45 } 46 47 bool judge(ll a, ll b) 48 { 49 g[1] = (a * f[1] + b) % mod; 50 for(int i = 2; i <=n; ++i) 51 { 52 int tp = (a * g[i - 1] + b) % mod; 53 if(tp != f[i]) return 0; 54 g[i] = (a * tp + b) % mod; 55 } 56 return 1; 57 } 58 59 int main() 60 { 61 n = read(); 62 for(int i = 1; i <= n; ++i) f[i] = read(); 63 for(int i = 1; i < mod; ++i) 64 { 65 ll x, y, a = i + 1, b = mod, d = f[2] - i * i * f[1], Gcd; 66 exgcd(a, b, x, y, Gcd); 67 if(d % Gcd) continue; 68 x = x * d / Gcd % mod; 69 if(judge(i, x)) 70 { 71 for(int j = 1; j <= n; ++j) write(g[j]), enter; 72 return 0; 73 } 74 } 75 return 0; 76 }