首页 > 代码库 > UVA 756 - Biorhythms(数论)

UVA 756 - Biorhythms(数论)

756 - Biorhythms

题目链接

基本就是裸的中国剩余定理。

代码:

#include <stdio.h>
#include <string.h>

const int M = 23 * 28 * 33;
const int m[3] = {23, 28, 33};
int p[3], d;

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

int main() {
	int cas = 0;
	while (~scanf("%d%d%d%d", &p[0], &p[1], &p[2], &d)) {
		if (p[0] < 0 && p[1] < 0 && p[2] < 0 && d < 0) break;		
  		int ans = 0;
    	for (int i = 0; i < 3; i++) {
			int x, y, w = M / m[i];
			gcd(m[i], w, x, y);
			ans = (ans + p[i] % m[i] * w * y) % M;
  		}
  		ans -= d;
  		if (ans <= 0) ans += M;
  		printf("Case %d: the next triple peak occurs in %d days.\n", ++cas, ans);
 	}
	return 0;
}