uva-12169&&hdu-2769-Disgruntled Judge 扩展欧几里得,模线性方程

2019-04-14 16:27发布

Disgruntled JudgeTime Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu SubmitStatusPracticeUVA 12169 Appoint description: Description Download as PDF 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 x1, x3, x5, ..., x2T-1 to the input file, and the sequence x2, x4, x6, ..., x2T to the output file. The random number generator the judge uses is quite simple. He picks three numbers x1, a, 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

枚举a,联立x2和x3的两个方程 x3 = (a * (a * x1 + b) % 10001 + b ) % 10001; x3 = (a * (a * x1 + b) + b) % 10001; 可得 x3 - a * a * x1 = (a + 1) * b + 10001 * (-k) 
即扩展欧几里得算法 解出一个b , 可以观察到b的通解为  b0+t*(mod/gcd(a+1, mod)),t为任意整数, 而mod是素数,因此gcd(a+1, mod)=1,有b=b0+t*mod,模mod后只有b0。
#include #include #include using namespace std; #define ll long long const int m=10001; int t; int len; int x[207]; ll c; int d, a, b, y; void gcd( int a, int b, int &d, int &x, int &y ) { if(!b) { d=a;x=1;y=0; } else{ gcd(b, a%b, d, y, x); y-=x*(a/b); } } bool judge() { for(int i=2;i<=len;i++){ if(i&1 && x[i] != ( (a * x[i - 1] %m+ b) % m) ){ //如果有一个输入项不合法,则a不合法 return false; } else { x[i]=((a * x[i - 1] %m+ b) )% m; } } return true; } void solve() { for(a=0; ; a++){ c=( ll ) x[3] - ( ll )a * a * x[1]; gcd(a+1, m, d, b, y); if(c%d) continue; //无解,a不合法 b=c/d * b%m; //取模m后的b if(judge()) return ; } } int main() { scanf("%d", &t); len=2*t; for(int i=1;i