Bailian4148 生理周期【枚举+中国剩余定理】

4148:生理周期
描述
人生来就有三个生理周期,分别为体力周期、感情周期和智力周期,它们的周期长度分别为23天、28天和33天。每一个周期中有一天是高峰。在高峰这天,人会在相应的方面表现出色。例如,在智力周期的高峰,人会思维敏捷,注意力容易高度集中。因为三个周期的长度不同,所以通常三个周期的高峰不会落在同一天。对于每个人,想知道何时三个高峰落在同一天。对于每个周期,会给出从当前年份的第一天开始,到出现高峰的天数(不一定是第一次高峰出现的时间)。给定一个从当年第一天开始的天数,你的任务是输出从给定时间开始(不包括给定时间),下一次三个高峰落在同一天的时间(距给定时间的天数)。例如:给定时间为10,下次出现三个高峰同一天的时间是12,则输出2(注意这里不是3)。
输入
输入包含多组数据,每一组数据由四个整数组成,数据以-1 -1 -1 -1 结束。
对于四个整数p, e, i和d,p, e, i分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d是给定的时间,可能小于p, e或i。所有给定时间是非负的并且小于或等于365,所求的时间小于或等于21252。
输出
从给定时间起,下一次三个高峰同一天的时间(距离给定时间的天数)。
样例输入
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

问题链接Bailian4148 生理周期
问题描述:(略)
问题分析
    本题有两种解法,一是采用枚举法,但是需要进行优化,以便加快计算速度;二是使用中国剩余定理以及到扩展欧几里德算法来解决。
程序说明
    本题与参考链接是同一题,使用参考链接的程序提交也AC。
参考链接POJ1006 UVA756 UVALive5421 Biorhythms【中国剩余定理】
题记:(略)

AC的C语言程序(优化枚举)如下:

/* POJ1006 UVA756 UVALive5421 Biorhythms */

#include <stdio.h>

int main(void)
{
    int p, e, i, d;
    int date, caseno=1;
    while(scanf("%d%d%d%d", &p, &e, &i, &d)) {
        if(p == -1)
            break;
        date = d;
        d++;
        while((d - p) % 23 != 0)
            d++;
        while((d - e) % 28 != 0 || (d - i) % 33 != 0)
            d += 23;
        printf("Case %d: the next triple peak occurs in %d days.\n", caseno++, d - date);
    }

    return 0;
}

AC的C语言程序(中国剩余定理)如下:

/* POJ1006 UVA756 Biorhythms */

#include <stdio.h>

// 递推法实现扩展欧几里德算法
long exgcd(long a, long b, long  *x, long *y)
{
    long x0=1, y0=0, x1=0, y1=1;
    long r, q;
    *x=0;
    *y=1;

    r = a % b;
    q = (a - r) / b;
    while(r)
    {
        *x = x0 - q * x1;
        *y = y0 - q * y1;
        x0 = x1;
        y0 = y1;
        x1 = *x;
        y1 = *y;

        a = b;
        b = r;
        r = a % b;
        q = (a - r) / b;
    }
    return b;
}

// 扩展欧几里德算法求逆元
long minv(long a, long p)
{
    long x, y;

    exgcd(a, p, &x, &y);

    return x<0 ? x+p : x;
}

// 中国剩余定理(Chinese remainder theorem, CRT)
long crt(long a[], long m[], int n)
{
    long bm=1, mi, x=0;
    int i;

    for(i=0; i<n; i++)
        bm *= m[i];
    for(i=0; i<n; i++) {
        mi = bm / m[i];
        x += a[i] * mi * minv(mi, m[i]);
        x %= bm;
    }

    return x;
}

int main(void)
{
    long p, e, i, d;
    long a[3], m[3];
    long x, bm;
    int ncase = 1;
    for(;;) {
        scanf("%ld%ld%ld%ld", &p, &e, &i, &d);
        if(p==-1 && e==-1 && i==-1 && d==-1)
            break;
        a[0] = p;
        a[1] = e;
        a[2] = i;
        m[0] = 23;
        m[1] = 28;
        m[2] = 33;

        bm = 23 * 28 * 33;
        x = crt(a, m, 3);
        while(x<=d)
            x += bm;

        printf("Case %d: the next triple peak occurs in %ld days.\n", ncase++, x-d);
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/85088199