扩展欧几里德定理

hdu 4596 Yet another end of the world

第一次接触这类题目,主要借鉴该博文 http://blog.csdn.net/pi9nc/article/details/22911885
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int MAXN = 1005;
int x[MAXN], y[MAXN], z[MAXN], n;
int gcd(int a, int b)
{
	return b?gcd(b,a%b):a;
}
int judge(int a, int l, int r)
{
	if (l%a == 0 || r%a == 0) return 1;
	if (l < 0 && r > 0) return 1;
	if (r/a - l/a >= 1) return 1;
	return 0;
}
int solve()
{
	for (int i = 0; i< n; ++i)
	{
		for (int j = i+1; j< n; ++j)
		{
			int g = gcd(x[i], x[j]);
			if (judge(g,y[i]-z[j],z[i]-y[j])) return 1;
		}
	}
	return 0;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
	while (scanf("%d", &n) != EOF)
	{
		for (int i = 0; i< n; ++i) scanf("%d%d%d", x+i, y+i, z+i);
		if (solve()) puts("Cannot Take off");
		else puts("Can Take off");
	}
	return 0;
}


hdu 1576 A/B

上题用扩展欧几里德定理中判别一阶不定方程是否有解,该题则在原来的基础上求方程的解;
p * a + q * b = gcd(a, b)   ......(0)
A = 9973*c + n
A = B*k
n = B * k - 9973*c   ......(1)
∵ gcd(B, 9973) == 1
∴ 方程(1)恒有解
先找出一组解 p0, q0(p0为0附近最小解)
则推出其他的解为:
p = p0 + b/gcd(a,b)*t
q = q0 + a/gcd(a,b)*t
在找出第一组解 k0,c0后(k0可能小于0),ki = k0+9973*t;
∵ 最后结果为 k % 9973
∴ return (k0%mod + mod)%mod*n%mod
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<set>
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
typedef __int64 ll;
const ll mod = 9973;
template<class T>
T exGCD(T a, T b, T &x, T &y)
{
	if (b == 0)
	{
		x = 1;
		y = 0;
		return a;
	}
	T r = exGCD(b,a%b,x,y);
	T t = x;
	x = y;
	y = t-a/b*y;
	return r;
}
int main()
{
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
	int t;
	scanf("%d", &t);
	while (t--)
	{
		ll n, B, k, c;
		scanf("%I64d%I64d", &n, &B);
		exGCD(B, mod, k, c);
		k = (k%mod + mod)%mod;
		printf("%I64d\n", n*k%mod);
	}
	return 0;
}


你可能感兴趣的:(扩展欧几里德定理)