HDU 2769 Disgruntled Judge 【扩展欧几里得】


传送门:HDU 2769


Disgruntled Judge
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Problem 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 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




题意:
比赛的时候题意理解错了,以为是输入一个数,对应的输出一个数,其实不是。
题目给你一个序列,这个序列给你n个数,分别为x1,x3,x5…,给你xi和xi-1的关系式,让你求出x2,x4,x6…


题解:
这题真正的解法是用扩展欧几里得做,但是偏偏这题用两次for循环暴力枚举每个a,b的情况也是可以过的= _ = ,暴力大法妙啊。

看完题目我们首先想到的应该是由已知的式子求出a,b的值,这样我们就可以直接递推求后面的式子就行了,但是a的值却并不好求。我们来看看题目给的递推式:

x i = ( a ∗ x i − 1 + b ) m o d 10001 x_i=(a*x_{i-1}+b)mod10001 xi=(axi1+b)mod10001

我们可以得到:
x 2 = ( a ∗ x 1 + b ) m o d 10001 x_2=(a*x_{1}+b)mod10001 x2=(ax1+b)mod10001
x 3 = ( a ∗ x 2 + b ) m o d 10001 x_3=(a*x_{2}+b)mod10001 x3=(ax2+b)mod10001

x 2 x_2 x2带入 x 3 x_3 x3可得:

x 3 = ( a 2 ∗ x 1 + ( a + 1 ) ∗ b ) m o d 10001 x_3=(a^2*x_{1}+(a+1)*b)mod10001 x3=(a2x1+(a+1)b)mod10001

即:
x 3 − a 2 ∗ x 1 = ( a + 1 ) ∗ b + k ∗ 10001 x_3-a^2*x_1=(a+1)*b+k*10001 x3a2x1=(a+1)b+k10001

因为a最大104,所以我们就可以枚举a的值,用扩展欧几里得求出b,如果用得到的a,b来检验后面的式子,如果都正确,那么这个a,b就是我们要的,x序列也求出来了。



AC代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int mod=10001;
int t;
ll nums[250];
ll exgcd(int a,int b,ll &x,ll &y)//返回的是最大公约数
{
  if(b==0)
  {
    x=1;
    y=0;
    return a;
  }
  ll r=exgcd(b,a%b,x,y);
  int t=x;
  x=y;
  y=t-(a/b)*y;
  return r;
}
int main()
{
  scanf("%d",&t);
  for(int i=1;i<=2*t;i+=2)
  {
    scanf("%I64d",&nums[i]);
  }
  int flag=1;
  for(int a=0;a<=10000;a++)
  {
    ll gd,x,y;
    gd=exgcd(a+1,mod,x,y);
    if((nums[3]-a*a*nums[1])%gd==0)
    {
      flag=1;
      ll b=x*((nums[3]-a*a*nums[1])/gd);
      for(int i=2;i<=2*t;i+=2)
      {
        nums[i]=(a*nums[i-1]+b)%mod;
        if(nums[i+1]!=(a*nums[i]+b)%mod&&i+1<=2*t)
        {
          flag=0;
          break;
        }
      }
    }
    if(flag) break;
  }
  for(int i=2;i<=2*t;i+=2)
  {
    printf("%I64d\n",nums[i]);
  }
  return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值