例1.5-2Biorhythms

问题描述:

人的体力,情感,智力得周期分别为23,28,33.一个周期内有一天为峰值。现在给出三个日期,分别对应于体力智力情感出现峰值的日期。再给出一个起始日期,要求从这一天开始,算出再过多少天后三个峰值会重新出现。

分析:

任意峰值之间相差整数倍,假设一年的第N天达到峰值,则下次峰值时间为N+kT;则三个峰值同时出现的那一天满足:

S=N1+k1*T1=N2+k2*T2=N3+k3*T3

利用中国剩余定理直接求出S,S-起始天数即为所求。

代码:

#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define LL long long int
using namespace std;
LL c[4],m[4];
void exgcd(LL a,LL b,LL &x,LL &y){
    if (b==0){
        x=1;
        y=0;
        return ;
    }
    exgcd(b,a%b,x,y);
    int t=x;
    x=y;
    y=t-(a/b)*y;
}
LL CRT(){
    LL ans=0;
    LL M=1;
    for (LL i=1;i<=3;i++){
        M*=m[i];
    }
    for (LL i=1;i<=3;i++){
        LL Mi=M/m[i];
        LL x,y;
        exgcd(Mi,m[i],x,y);
        ans=(ans+c[i]*Mi*x)%M;
    }
    if (ans<0)ans+=M;
    return ans;
}
int main(){
    LL d;
    LL cas=0;
    m[1]=23,m[2]=28,m[3]=33;
    while (scanf("%lld%lld%lld%lld",&c[1],&c[2],&c[3],&d)!=EOF){
        if (c[1]==-1&&c[2]==-1&&c[3]==-1&&d==-1)break;
        LL fun=CRT();
        if (fun==0)fun=21252;
        if (fun<=d)fun+=21252;
        d=fun-d;
        printf("Case %lld: the next triple peak occurs in %lld days.\n",++cas,d);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值