扩展欧几里得应用解不定方程(uva12169,Disgruntled Judge,NWERC 2008)

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

简单的翻译一下,就是告诉你1357这样的序列,根据上面的规则,求2468这样的序列。

我们将方程求解并且化简,可以得到x3 - a * a * x1 = (a + 1) * b + 10001 * (-k);这就是裸的扩展欧几里得定律啦~

然后抄上模板,可以把b解出来,最后代入验证是否都符合

#include<bits/stdc++.h>
#define mod 10001
#define LL unsigned long long
using namespace std;
int f[200000];

void gcd(LL a,LL b,LL &d,LL &x,LL &y)
{
	if(!b) d=a,x=1,y=0;
	else {gcd(b,a%b,d,y,x);y-=x*(a/b);}
}

int main()
{
	LL t;
	cin>>t;
	for(LL i=1;i<2*t;i+=2) cin>>f[i];
	LL a;
	for(LL i=0;i<mod;i++)
	{
		a=i;bool ok=1;
		LL d,k,b;
		LL tmp=(f[3]-a*a*f[1]);
		gcd(mod,a+1,d,k,b);
		if(tmp%d) continue;
		b=b*tmp/d;//是倍数等比例扩大一下important!
		for(LL j=2;j<=2*t;j++)
		{
			if(j&1)//表示是奇数 
			{
				if(f[j]!=((a*f[j-1]+b)%mod))
				{
					ok=0;break;
				}
			}
			else f[j]=(a*f[j-1]+b)%mod;
			
		} if(ok) break;//如果发现a之后就不用再继续搜索了 
	}
	for(LL j=2;j<=2*t;j+=2)
		cout<<f[j]<<endl;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值