POJ1006 Biorhythms

问题描述
人有三个生理周期:体能(23天),情感(28天),智力(33天),每个周期都有能力达到顶峰的一天。现在给出三个能力的顶峰日(如288),以及当前的时间(如555),求之后下一个全能力顶峰日距离现在还有多少。

输入
多组数据,一行为四个整数P,E,I,d,对应体能顶峰日、情感顶峰日、智力顶峰日、当前时间。
当输入-1 -1 -1 -1时结束程序

输出
对于第一组数据(答案1234时),我们输出:
Case 1: the next triple peak occurs in 1234 days.
这样的句子。

样例输入
0 0 0 0
0 0 0 100
5 20 34 325
4 5 6 7
283 102 23 320
203 301 203 40
-1 -1 -1 -1

样例输出
Case 1: the next triple peak occurs in 21252 days.
Case 2: the next triple peak occurs in 21152 days.
Case 3: the next triple peak occurs in 19575 days.
Case 4: the next triple peak occurs in 16994 days.
Case 5: the next triple peak occurs in 8910 days.
Case 6: the next triple peak occurs in 10789 days.

来源
East Central North America 1999

代码

//作者:Chorolop
#include<cstdio>
#include<iostream>
using namespace std;

void ex_gcd(int a,int b,int &d,int &x,int &y) //扩展欧几里德
{
    if(b == 0){x = 1,y = 0,d = a;}
    else{ex_gcd(b,a%b,d,y,x);y -= x*(a/b);}
}

int CRT(int n,int m[],int a[]) //中国剩余定理
{
    int M = 1,Mi,ans = 0;

    for(int i = 1;i <= n;i++) //求M
        M *= m[i];

    for(int i = 1;i <= n;i++)
    {
        int x,y,d;
        Mi = M/m[i];
        ex_gcd(Mi,m[i],d,x,y); //扩展欧几里德求逆元
        ans = (ans+x*Mi*a[i])%M;
    }
    if(ans<0) ans += M;
    return ans;
}

int main() //主函数都是套路
{
    int a[4],m[4] = {0,23,28,33},start,c = 1;
    scanf("%d%d%d%d",&a[1],&a[2],&a[3],&start);
    while(a[1]!=-1 && a[2]!=-1 && a[3]!=-1 && start!=-1)
    {

        int ans = CRT(3,m,a);
        if(ans <= start) ans += 21252;
        printf("Case %d: the next triple peak occurs in %d days.\n",c++,ans-start);

        scanf("%d%d%d%d",&a[1],&a[2],&a[3],&start);
    }
}

评价
代码没什么难度,主要需要掌握几个理论基础:中国剩余定理+扩展欧几里德+逆元

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值