UVA 12169 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 Ttest 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

一.分析

这题的主要思想是枚举了a,然后用数学推导转化成拓展欧几里德的问题,取模的问题感觉极本都是拓展欧几里德来解决的,还有很重要一点要注意,最好把可以的变量都开成long long 按理说每个值都对10001取模应该int是可以储存的,但是开了long long 才过的

  main.cpp
  UVa12169

  Created by 张嘉韬 on 16/8/1.
  Copyright © 2016年 张嘉韬. All rights reserved.

//
//#include <iostream>
//#include <cstring>
//#include <cstdio>
//#include <cmath>
//using namespace std;
//typedef long long ll;
//const int maxn=10000+10;
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(b==0)
    {
        x=0;
        y=1;
        return a;
    }
    ll r=exgcd(b,a%b,y,x);
    y-=x*a/b;
    return r;
}
//
//ll exgcd(ll a,ll b,ll &x,ll &y)
//{
//    ll r,t;
//    if(!b){ x=1; y=0; return a;}
//    r=exgcd(b,a%b,x,y);
//    t=x;
//    x=y;
//    y=t-a/b*y;
//    return r;
//}
//int main(int argc, const char * argv[]) {
//    freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin);
//    int A[maxn]={0};
//    int r[maxn]={0};
//    int n;
//    cin>>n;
//    for(int i=1;i<=n;i++) cin>>A[i];
//    for(int a=0;a;a++)
//    {
//        ll tc,g,b;
//        int flag=1;
//        g=exgcd(a+1,10001,b,tc);
//        if((A[2]-a*a*A[1])%g!=0) continue;
//        b=b*((A[2]-a*a*A[1])/g);
//        b=(b%(10001/g)+(10001/g))%(10001/g);
//        r[1]=(a*A[1]+b)%10001;
//        for(int i=2;i<=n;i++)
//        {
//            if(A[i]!=(a*r[i-1]+b)%10001)
//            {
//                flag=0;
//                break;
//            }
//            r[i]=(a*A[i]+b)%10001;
//        }
//        if(flag==0) continue;
//        else break;
//    }
//    for(int i=1;i<=n;i++)
//    {
//        cout<<r[i]<<endl;
//    }
//    return 0;
//}
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const int maxn=10000+10;
//ll exgcd(ll a,ll b,ll &x,ll &y)
//{
//    if(b==0)
//    {
//        x=0;
//        y=1;
//        return a;
//    }
//    ll r=exgcd(b,a%b,y,x);
//    y-=(x*(a/b));
//    return r;
//}

ll exgcd(ll a,ll b,ll &x,ll &y)
{
    ll r,t;
    if(!b){ x=1; y=0; return a;}
    r=exgcd(b,a%b,x,y);
    t=x;
    x=y;
    y=t-a/b*y;
    return r;
}
int main(int argc, const char * argv[]) {
    freopen("/Users/zhangjiatao/Documents/暑期训练/input.txt","r",stdin);
    ll A[maxn]={0};
    ll r[maxn]={0};
    int n;
    cin>>n;
    for(int i=1;i<=n;i++) cin>>A[i];
    for(int a=0;a<=10000;a++)
    {
        ll tc,g,b;
        int flag=1;
        g=exgcd(a+1,10001,b,tc);
        if((A[2]-a*a*A[1])%g!=0) continue;
        b=b*((A[2]-a*a*A[1])/g);
        b=(b%(10001/g)+(10001/g))%(10001/g);
        r[1]=(a*A[1]+b)%10001;
        for(int i=2;i<=n;i++)
        {
            if(flag==0) break;
            if(A[i]!=(a*r[i-1]+b)%10001)
            {
                //cout<<"shit"<<endl;
                flag=0;
                break;
            }
            r[i]=(a*A[i]+b)%10001;
        }
        if(flag==0) continue;
        else break;
    }
    for(int i=1;i<=n;i++)
    {
        cout<<r[i]<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值