惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
C
Check Point Blog
博客园_首页
B
Blog
D
Docker
U
Unit 42
量子位
I
InfoQ
有赞技术团队
有赞技术团队
Martin Fowler
Martin Fowler
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
博客园 - Franky
美团技术团队
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
月光博客
月光博客
Vercel News
Vercel News
Recent Announcements
Recent Announcements
雷峰网
雷峰网
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Google DeepMind News
Google DeepMind News

博客园 - saintqdd

hdu 1102 pku 2421 解题报告 pku 2777 Count Color 解体报告 石子合并问题 nkoj1139和乘积最大那题一样. A Tour in Loquat Orchard (FZU 2007 ICPC Qualification Round I tzw) 最大黑区域 滑雪 一道经典题,humble number 今天碰到了一个很诡异的题,Alphacode (zoj 2202) 这两天经常碰到dp题,就写了一个0-1背包 实训以来,到这里的次数少了! 郁闷,乘积最大那题WA原来只是因为我用了pow函数引起的! Smith Number POJ强烈推荐50题 JOJ 2391 words POJ 1014 三十分钟掌握STL STL学习小记 POJ1003,简单题
POJ1006,中国剩余定理
saintqdd · 2007-08-04 · via 博客园 - saintqdd

此题是中国剩余定理的典型应用,具体解法是:

对于给定的p,e,i,他们分别是除23,28,33的余数。问题就是求除23余p,除28余e,除33余i,的最小数。是不是很熟悉啊,当然你也可以手算然后在用程序输出,不过这样做就没什么意思了。先求除23余p,除28余e的最小公共数。for循环求出p+23*i==e+28*j,即最小数minpe=p+23*i,公共数为minpe+gcd(23,28)*x,相同的方法在与最后一个数进行条件合并,最后可求得minpei+gcd(gcd(23,28),33)*x;我们想要的就是minpei+gcd(gcd(23,28),33);解毕.

POJ1006 code:

#include<iostream>
#define physical 23
#define emotional 28
#define mental 33

int gcd(int a,int b){
    int t1=a,t2=b,t;
    while(a%b!=0){
        t=a%b;
        a=b;
        b=t;
    }
    return (t1*t2)/b;
}
int main(){
    int p,e,i,d,j,k,P=1;
    while(scanf("%d%d%d%d",&p,&e,&i,&d)&&!(p==-1&&e==-1&&i==-1&&d==-1)){
        int pe=gcd(physical,emotional),minpe;

        for(j=0,k=0;;){
            if((p+physical*j)==(e+emotional*k))
                break;
            if((p+physical*j)<(e+emotional*k))
                j++;
            if((p+physical*j)>(e+emotional*k))
                k++;
        }

        minpe=p+physical*j;
        int pei=gcd(pe,mental),minpei;
        for(j=0,k=0;;){
            if((minpe+pe*j)==(i+mental*k))
                break;
            if((minpe+pe*j)<(i+mental*k))
                j++;
            if((minpe+pe*j)>(i+mental*k))
                k++;
        }       
        minpei=i+mental*k;
        if((minpei+pei-d)%pei==0)
            printf("Case %d: the next triple peak occurs in %d days.\n",P++,pei);
        else
            printf("Case %d: the next triple peak occurs in %d days.\n",P++,(minpei+pei-d)%pei);
    }
}