POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)

本文介绍了解决POJ.1006Biorhythms问题的方法,通过使用拓展欧几里得算法与中国剩余定理来找到特定日期。代码实现了输入周期并计算下一个三重峰值出现的日期。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

POJ.1006 Biorhythms (拓展欧几里得+中国剩余定理)

题意分析

(1)

不妨设日期为x,根据题意可以列出日期上的方程:

(2)

化简可得:

(3)

根据中国剩余定理求解即可。

代码总览

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
using namespace std;
typedef int ll;
ll p,e,i,d;
void exgcd(ll a, ll b, ll& d, ll& x, ll &y)
{
    if(!b){
        d = a,x = 1,y = 0;
    }else{
        exgcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}
int CRT(int a[], int m[])
{
    int ans = 0;
    int M = 1;
    int x,y,gcd;
    for(int i = 1; i<=3;++i)M*=m[i];
    for(int i =1 ;i<=3;++i){
        int Mi = M/m[i];
        exgcd(Mi,m[i],gcd,x,y);
        ans = (ans + Mi * a[i] * x) % M;
    }
    if(ans<=0) ans+=M;
    if(ans<d) ans+=M;
    return ans;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int kase = 0;
    while(scanf("%d %d %d %d",&p,&e,&i,&d)){
        if(p == -1 && e == -1 && i == -1 && d == -1) break;
        int a[] = {0,p,e,i};
        int m[] = {0,23,28,33};
        int ans = CRT(a,m);
        printf("Case %d: the next triple peak occurs in %d days.\n",++kase,ans-d);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值