UVa 756 - Biorhythms

本文通过数论和中国剩余定理解决了一个关于三种不同生物周期(23天、28天、33天)的问题,旨在找到这三种周期下一次同时达到峰值的具体日期。使用扩展的欧几里得算法来寻找逆元。

题目

生物周期,三种属性的生物周期为23,28,和33天。已知每个周期出现的某个日期,现在是第d天,问第多少天三种周期同时到达。

分析

数论,中国剩余定理。用扩展的欧几里得求逆元。

说明

经典例题。。。

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

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

int main()
{
	int d, Mi, ti, y, k, cases = 1;
	int r[3], m[3] = {23, 28, 33};
	while (~scanf("%d%d%d%d", &r[0], &r[1], &r[2], &d) && d != -1) {
		int M = m[0] * m[1] * m[2], flag = 1, ans = 0;
		for (int i = 0; i < 3; ++ i) {
			Mi = M/m[i];
			exgcd(Mi, m[i], k, ti, y);
			if (ti < m[i])
				ti += m[i];
			ans = (ans + Mi * ti * r[i]) % M;
		}
		while (ans <= d) {
			ans += M;
		}
		ans -= d;
		printf("Case %d: the next triple peak occurs in %d days.\n", cases ++, ans);
	} 
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值