您的位置:首页 > 其它

POJ 1006 Biorhythms 中国剩余定理

2014-05-12 16:32 302 查看
题目来源:POJ 1006 Biorhythms

题意:给出3个周期第一次发生的时间 和 当前开始的天数 求三个周期下一次到达高峰期发生在哪一天

思路:这题很水 试一下我的模版而已

#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn = 10;
int a[maxn], m[maxn];
//求整数x和y,使得ax+by=d, 且|x|+|y|最小。其中d=gcd(a,b) 
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);
	}
}
LL china(int n, int* a, int* m)
{
	LL M = 1, d, y, x = 0;
	for(int i = 0; i < n; i++)
		M *= m[i];
	for(int i = 0; i < n; i++)
	{
		LL w = M /m[i];
		gcd(m[i], w, d, d, y);
		x = (x + y*w*a[i]) % M;
	}
	return (x+M)%M;
}
int main()
{
	
	m[0] = 23;
	m[1] = 28;
	m[2] = 33;
	int cas = 1;
	int n;
	while(scanf("%d %d %d %d", &a[0], &a[1], &a[2], &n) != EOF)
	{
		if(a[0] == -1 && a[1] == -1 && a[2] == -1 && n == -1)
			break;
		int ans = china(3, a, m)-n;
		if(ans <= 0)
			ans += 21252;
		if(ans > 21252)
			ans %= 21252;
		printf("Case %d: the next triple peak occurs in %d days.\n", cas++, ans);
	}
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: