NC50576. Biorhythms
描述
输入描述
本题有多组数据。
对于每组数据,输入四个整数p,e,i和d。p,e,i分别表示体力、情感和智力高峰出现的时间(时间从当年的第一天开始计算)。d是给定的时间,可能小于p,e或i。
当p=e=i=d=-1时,输入数据结束。
输出描述
从给定时间起,下一次三个高峰同天的时间(距离给定时间的天数)。采用以下格式:
Case 1: the next triple peak occurs in 1234 days.
.注意:即使结果是1天,也使用复数形式days。
示例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.
C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 496K, 提交时间: 2019-08-26 16:47:48
#include<bits/stdc++.h> using namespace std; typedef long long ll; ll m[5],a[5]; void exgcd(ll a,ll b,ll &d,ll &x,ll &y) { if(b==0){ d=a; x=1,y=0; } else{//else不能省略 exgcd(b,a%b,d,y,x); y-=(a/b)*x; } } //中国剩余定理 ll China(int n,ll *m,ll *a) { ll M=1,d,y,x=0; for(int i=0;i<n;i++) M*=m[i]; for(int i=0;i<n;i++){ ll w=M/m[i]; exgcd(m[i],w,d,d,y); x=(x+y*w*a[i])%M; } return (x+M)%M; } int main(){ m[0]=23,m[1]=28,m[2]=33; int d,p,e,i,ca=1; while(~scanf("%d%d%d%d",&p,&e,&i,&d)&&p!=-1&&e!=-1&&d!=-1&&i!=-1){ a[0]=p; a[1]=e; a[2]=i; ll ans=China(3,m,a)-d; if(ans<=0) ans+=21252; printf("Case %d: the next triple peak occurs in %lld days.\n",ca++,ans); } }
C(clang11) 解法, 执行用时: 13ms, 内存消耗: 500K, 提交时间: 2021-04-23 11:47:05
#include <stdio.h> int main() { int p,e,i,d,j,c=0; while(scanf("%d%d%d%d",&p,&e,&i,&d)!=EOF) { if(p==-1&&e==-1&&i==-1&&d==-1) break; for(j=0;j<=21252;j++) if((j-p)%23==0&&(j-e)%28==0&&(j-i)%33==0) break; if(j-d<=0) j+=21252; printf("Case %d: the next triple peak occurs in %d days.\n",++c,j-d); } return 0; }
C++11(clang++ 3.9) 解法, 执行用时: 3ms, 内存消耗: 400K, 提交时间: 2020-10-12 17:15:58
#include<bits/stdc++.h> using namespace std; const int M=21252; int main(){ for(int p,e,i,d,ans,t=0;;){ scanf("%d%d%d%d",&p,&e,&i,&d); if(p==-1)return 0; ans=(5544*p+14421*e+1288*i-d+M)%M; if(ans==0)ans=M; printf("Case %d: the next triple peak occurs in %d days.\n",++t,ans); } }