Disgruntled Judge(扩展欧几里得)

本文介绍了一个随机生成问题的解决思路及实现代码。该问题中,输入为一系列随机生成的奇数项,需根据特定公式推导出偶数项。文章详细分析了如何通过已知条件建立方程组并运用扩展欧几里得算法求解未知参数。

Disgruntled Judge

 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
题意:

随机选取x1,a,bx1,a,b根据公式xi=(xi1+b)%10001xi=(xi−1+b)%10001 得到一个长度为2n的序列,奇数项作为输入,偶数项作为输出

现在给你奇数项,让你求出偶数项

分析:

首先奇数项知道了,偶数项不知道我们根据公式可以得到下面的方程组

ax1+bx2=my1ax1+b−x2=my1

ax2+bx3=my2ax2+b−x3=my2

因为偶数项不知道,所以我们要消去偶数项

一式两边同乘a然后两式相加化简得到

(a+1)b+my=x3a2x1(a+1)b+my=x3−a2x1

这时我们可以枚举a

这样我们就得到了关于b和y的线性方程

使用扩展欧几里得算法可以求出b

然后根据得到的b我们生成序列看是否符合题目公式要求(奇数项已经固定了我们要通过奇数项生成偶数项,在检测偶数项能否得到下一个奇数项)

code:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
const int mod = 10001;
ll x[222];
ll ex_gcd(ll a,ll b,ll &x,ll &y){
    if(!b){
        x = 1;
        y = 0;
        return a;
    }
    ll d = ex_gcd(b,a%b,y,x);
    y -= a / b * x;
    return d;
}

int main(){
    int n,i;
    while(~scanf("%d",&n)){
        n *= 2;
        for(int i = 1; i < n; i += 2){
            scanf("%lld",&x[i]);
        }
        ll a,b,c,d,y;
        for(a = 0; ; a++){
            c = x[3] - a * a * x[1];
            d = ex_gcd(a+1,mod,b,y);
            if(c % d) continue;
            b = b * c / d;
            for(i = 2; i <= n; i++){
                if(i & 1){
                    if(x[i] != (a * x[i-1] + b) % mod) break;
                }
                else{
                    x[i] = (a * x[i-1] + b) % mod;
                }
            }
            if(i > n) break;
        }
        for(i = 2; i <= n; i += 2){
            printf("%lld\n",x[i]);
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值