例题10-2 不爽的裁判 UVa12169

1.题目描述:点击打开链接

2.解题思路:本题要求找到合适的一组a,b,使得按照递推公式能输出正确的x2,x4...可以枚举a值,通过列写方程得到b的值,但这里有一个问题,这里是一个同余方程,等号的一端带有k*10001,这时就应该迅速的想到利用扩展gcd来解决,已知量为1001,a+1,求出gcd(10001,a+1)以及线性方程的系数x,y即可。当发现计算出的数和原来的输入矛盾时,说明a是非法的。由于a介于0~10000之间,因此即使枚举所有的a,效率也足够高。

3.代码:

#define _CRT_SECURE_NO_WARNINGS 
#include<iostream>
#include<algorithm>
#include<string>
#include<sstream>
#include<set>
#include<vector>
#include<stack>
#include<map>
#include<queue>
#include<deque>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#include<functional>
using namespace std;

#define maxn 10001
typedef long long LL;
int x[maxn];
LL a, b;
void gcd(LL a, LL b, LL&d, LL&x, LL&y)
{
	if (b == 0){ d = a, x = 1, y = 0; }
	else
	{
		gcd(b, a%b, d, y, x);
		y -= x*(a / b);
	}
}
int main()
{
	//freopen("test.txt", "r", stdin);
	int T;
	cin >> T;
	for (int i = 1; i <= 2 * T - 1; i += 2)
		cin >> x[i];
	for (a = 0; a < maxn; a++)
	{
		LL tmp = (LL)x[3] - a*a*x[1];
		LL d, xx;
		gcd(maxn, a + 1, d, xx, b);
		if (tmp % d != 0)	continue;
		b = b*(tmp/d);
		int y[maxn];
		int ok = 1;
		memset(y, 0, sizeof(y));
		y[1] = x[1];
		for (int i = 2; i <= 2 * T; i++)
		{
			y[i] = (a*y[i - 1] + b) % maxn;
			if (x[i]>0)
			if (x[i] != y[i]){ ok = 0; break; }
		}
		if (!ok)continue;
		else
		{
			memcpy(x, y, sizeof(y));
			for (int i = 2; i <= 2 * T; i += 2)
				cout << x[i] << endl;
			break;
		}
	}
	return 0;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值