Uva--11269(数论,拓展欧几里得,模运算)

2014-09-01 15:04:05

12169 - Disgruntled Judge

Time limit: 3.000 seconds

Once upon a time, there was an nwerc judge with a tendency to create slightly too hard problems. As a result, his problems were never solved. As you can image, this made our judge somewhat frustrated. This year, this frustration has culminated, and he has decided that rather than spending a lot of time constructing a well-crafted problem, he will simply write some insanely hard problem statement and just generate some random input and output files. After all, why bother having proper test data if nobody is going to try the problem anyway?

Thus, the judge generates a testcase by simply letting the input be a random number, and letting the output be another random number. Formally, to generate the data set with T test cases, the judge generates 2T random numbers x1, ..., x2T between 0 and 10000, and then writes T, followed by the sequence x1x3x5, ..., x2T-1 to the input file, and the sequence x2x4x6, ..., x2T to the output file.

The random number generator the judge uses is quite simple. He picks three numbers x1a, and b between 0 and 10000 (inclusive), and then for i from 2 to 2T lets xi = (a · xi-1 + b) mod 10001.

You may have thought that such a poorly designed problem would not be used in a contest of such high standards as nwerc. Well, you were wrong.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

  • One line containing an integer n (0 ≤ n ≤ 10000): an input testcase.

The input file is guaranteed to be generated by the process described above.

Output

Per testcase:

  • One line with an integer giving the answer for the testcase.

If there is more than one output file consistent with the input file, any one of these is acceptable.

Sample Input

3
17
822
3014

Sample Output

9727
1918
4110
The 2008 ACM Northwestern European Programming Contest
 
思路:由 x2 ≡ a * x1 + b (mod 10001),x3 ≡ a * x2 + b (mod 10001),将x2带入第二条式子中:x3 - a^2 * x1 ≡ (a + 1) * b (mod 10001),
  建立拓展欧几里得线性方程:(a + 1) * b' - 10001 * y = c (形式:ax + by = gcd(a,b) = c),其中b' = b * c / (x3 - a^2 * x1)。
  总的过程是枚举a,然后检查时候满足数列。感谢http://blog.csdn.net/u012760629/article/details/38084863
 1 /*************************************************************************
 2     > File Name: 12169.cpp
 3     > Author: Nature
 4     > Mail: 564374850@qq.com
 5     > Created Time: Sun 31 Aug 2014 10:34:46 AM CST
 6 ************************************************************************/
 7 
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cstdlib>
11 #include <cmath>
12 #include <iostream>
13 #include <algorithm>
14 using namespace std;
15 const int mod = 10001;
16 
17 int t,v[205];
18 
19 bool Judge(int a,int b){
20     int top = t << 1;
21     for(int i = 2; i <= top; ++i){
22         if(i & 1){
23             if(v[i] != (a * v[i - 1] % mod + b) % mod){
24                 return false;
25             }
26         }
27         else{
28             v[i] = (a * v[i - 1] % mod + b) % mod;
29         }
30     }
31     return true;
32 }
33 
34 void Print(){
35     int top = t << 1;
36     for(int i = 2; i <= top; i += 2)
37         printf("%d\n",v[i]);
38 }
39 
40 void Ex_gcd(int a,int b,int &d,int &x,int &y){
41     if(!b) d = a,x = 1,y = 0;
42     else Ex_gcd(b,a % b,d,y,x),y -= a / b * x;
43 }
44 
45 int main(){
46     scanf("%d",&t);
47     for(int i = 1; i <= t; ++i)
48         scanf("%d",&v[(i << 1) - 1]);
49     int a,b,c,x,y,d;
50     for(a = 0; a <= 10000; ++a){
51         Ex_gcd(a + 1,-mod,d,b,y);
52         c = (mod + v[3] - (a * a % mod) * v[1] % mod) % mod;
53         if(c % d) continue; //非整除 无解。
54         b *= c / d;
55         int k = abs(-mod / d);
56         b = (b % k + k) % k;
57         if(Judge(a,b))  break;
58     }
59     Print();
60     return 0;
61 }

 

posted @ 2014-09-01 15:15  Naturain  阅读(199)  评论(0编辑  收藏  举报