HDU 1370: Biorhythms

1

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.

分析

可以维护一个bool数组暴力模拟,也可以中国剩余定理。

(n+d)%23=a,(n+d)%28=b,(n+d)%33=c;

所以,n=(33*28*6*p+23*33*19*e+1288*i)%lcm(23,28,33)=n+d

即ans=(5544p+14421e+1288i-d)%21252

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define range(i,a,b) for(int i=a;i<=b;++i)
#define rerange(i,a,b) for(int i=a;i>=b;--i)
#define LL long long
#define CLS(arr) memset(arr,0,sizeof(arr))
using namespace std;
bool days[21255];
int p,e,i,d;
void solve(){
    int ans,cnt=0;
    while(cin>>p>>e>>i>>d,p!=-1&&e!=-1&&i!=-1&&d!=-1){
        CLS(days);
        for(int k=p;k<=21252;k+=23)days[k]=true;
        range(k,0,21252)days[k]=(k-e)%28?false:days[k]?true:false;
        range(k,0,21252)days[k]=(k-i)%33?false:days[k]?true:false;
        range(k,d+1,21252)if(days[k]){ans=k;break;}
        cout<<"Case "<<++cnt<<": the next triple peak occurs in "<<ans-d<<" days."<<endl;
    }
}
int main(int argc, char *argv[]){
    solve();
    return 0;
}
View Code

 

posted @ 2018-07-17 11:49  RhythmLian  阅读(161)  评论(0编辑  收藏  举报