您的位置:首页 > 其它

UVA 756 - Biorhythms(数论)

2014-06-25 19:37 274 查看


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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: