POJ 1006 Biorhythms【CRT】

题目:http://poj.org/problem?id=1006

题意:
人自出生起就有体力,情感和智力三个生理周期,分别为23,28和33天。现在给出三个日期,分别对应于体力,情感,智力出现峰值的日期,然后按照周期依次出现。然后再给出一个起始日期,要求从这一天开始,算出最少再过多少天后三个峰值同时出现。

分析:
CRT入门:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;

const int MAXN = 1e3 + 10;
int a[5], m[5];

void ex_gcd(int a, int b, int &x, int &y) {
    if(!b) {
        x = 1;
        y = 0;
        return ;
    }
    ex_gcd(b, a % b, x, y);
    int tmp = x;
    x = y;
    y = tmp - (a / b) * y;
}
//板子
int CRT(int * a, int * m, int n) {
    int M = 1, ans = 0;
    for(int i = 1; i <= n; i++) {
        M *= m[i];
    }
    for(int i = 1; i <= n; i++) {
        int x, y;
        int Mi = M / m[i];
        ex_gcd(Mi, m[i], x, y);
        ans = (ans + Mi * a[i] * x) % M;
    }
    return ans;
}

int main() {
    int p, e, i, d;
    while(scanf("%d%d%d%d", &p, &e, &i, &d) && p != -1) {
        int x1 = 23, x2 = 28, x3 = 33;
        int M = x1 * x2 * x3;
        a[1] = p % x1;
        a[2] = e % x2;
        a[3] = i % x3;
        m[1] = x1;
        m[2] = x2;
        m[3] = x3;
        int ans = CRT(a, m, 3);
        if(ans <= d) ans += M;
        static int tt = 1;
        printf("Case %d: the next triple peak occurs in %d days.\n", tt++, ans - d);
    }
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值