uvaoj 12169 Disgruntled Judge 扩展欧几里得算法

16 篇文章 0 订阅
uvaoj 12169 Disgruntled Judge 扩展欧几里得算法
一个裁判,找了3个整数x1,a和b,按照递推公式xi=(axi-1+b)%10001,计算出了一个长度为2n的序列,n是测试数据的组数,然后他把n和x1,x3,。。。,x2n-1写到输入文件中,x2,x4,。。。,x2n写到输出文件中。你的任务是找出对应的x2,x4,。。。,x2n。任意输出一个合法的。
如果知道了a,根据x2=(ax1+b)%10001,x3=(ax2+b)%10001,联立可以得到x3=(a((ax1+b)%10001) + b)%10001=(a(ax1+b)+b)%10001也就是,x3+10001k=(a+1)b+a*a*x1.
可以写成(a+1)b+10001(-k)=x3-a*a*x2,这是不定方程的形式,可以用扩展欧几里得算法求出b。枚举a,求出b,然后求出整个序列,判断序列是否合法就可以了。复杂度为10^6。
代码如下:
/*************************************************************************
	> File Name: 12169.cpp
	> Author: gwq
	> Mail: gwq5210@qq.com 
	> Created Time: 2015年01月21日 星期三 20时08分53秒
 ************************************************************************/

#include <cmath>
#include <ctime>
#include <cctype>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <cstring>

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <algorithm>

#define INF (INT_MAX / 10)
#define clr(arr, val) memset(arr, val, sizeof(arr))
#define pb push_back
#define sz(a) ((int)(a).size())

using namespace std;
typedef set<int> si;
typedef vector<int> vi;
typedef map<int, int> mii;
typedef long long ll;
typedef unsigned long long ull;

const double esp = 1e-5;

#define N 110

ll num[N];

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

int main(int argc, char *argv[])
{
	ll n;
	scanf("%lld", &n);
	for (ll i = 1; i <= n; ++i) {
		scanf("%lld", &num[2 * i - 1]);
	}
	for (ll a = 0; a <= 10000; ++a) {
		ll d, x, y, t, b;
		exgcd(a + 1, 10001, d, x, y);
		t = num[3] - a * a * num[1];
		if (t % d) {
			continue;
		}
		x *= t / d;
		y *= t / d;
		b = x;
		ll flag = 0;
		for (ll i = 2; i <= 2 * n; ++i) {
			ll tmp = (a * num[i - 1] + b) % 10001;
			if (i % 2 == 0) {
				num[i] = tmp;
			} else {
				if (num[i] != tmp) {
					flag = 1;
					break;
				}
			}
		}
		if (!flag) {
			break;
		}
	}
	for (ll i = 1; i <= n; ++i) {
		printf("%lld\n", num[2 * i]);
	}

	return 0;
}

参考:http://blog.csdn.net/u013451221/article/details/38497029
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值