UVA12169 Disgruntled Judge(扩展欧几里得+枚举)

UVA - 12169 Disgruntled Judge

Description

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 les. 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 10 000, and then writes T, followed by the sequence x1, x3, x5, . . . , x2T −1 to the input le, and the sequence x2, x4, x6, . . . , x2T to the output le.
The random number generator the judge uses is quite simple. He picks three numbers x1, a, and b between 0 and 10 000 (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 rst 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 le 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 le consistent with the input le, any one of these is acceptable.

Sample Input

3
17
822
3014

Sample Output

9727
1918
4110

题解

题意

已知xi=(a*xi-1+b) mod 10001,且告诉你x1,x3.........x2*t-1,让你求出其偶数列

思路

由于mod的范围不超过10001,因此可以枚举a从0-10000。

利用扩展欧几里得算法计算出最后的结果:

枚举a,然后通过x1,x3求出b,再验证是否合适

1.设a, b, c为任意整数。若方程ax+by=c的一组整数解为(x0,y0),则它的任意整数解都可以写成(x0+kb', y0-ka'),其中a'=a/gcd(a,b),b'=b/gcd(a,b),k取任意整数。

2.设a, b, c为任意整数,g=gcd(a,b),方程ax+by=g的一组解是(x0,y0),则当c是g的倍数时ax+by=c的一组解是(x0c/g, y0c/g);当c不是g的倍数时无整数解。

扫描二维码关注公众号,回复: 3051529 查看本文章

代码

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const double PI = acos(-1.0);
const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

#define REP(i,n) for(int i=0;i<(n);i++)

const int MAXN = 100+10;
ll x[MAXN];
const int MOD = 10001;

ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0){
        x=1;
        y=0;
        return a;
    }
    ll r=exgcd(b,a%b,x,y);
    ll temp=x;
    x=y;
    y=temp-(a/b)*y;
    return r;
}

int main(){
    int T = 0;
    scanf("%d",&T);
    T = 2*T;
    for(int i=1;i<=T;i+=2){
        scanf("%lld",&x[i]);
    }
    for(int i=0;i<=10000;i++){
        ll a = i;
        ll c = x[3] - a*a * x[1];
        ll b,t;
        ll d = exgcd(a+1,MOD,b,t);
        if(c%d) continue;
        b = b*c/d;
        int j = 0;
        for(j=2;j<=T;j++){
            if(j&1){
                if(x[j]!=(a*x[j-1]+b)%MOD)  break;
            }else
                x[j]=(a*x[j-1]+b)%MOD;
        }
        if(j>T) break; 
    }
    for(int i=2;i<=T;i+=2)  printf("%lld\n",x[i]);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/caomingpei/p/9585204.html