Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 137615 | Accepted: 44029 |
Description
Since the three cycles have different periods, the peaks of the three cycles generally occur at different times. We would like to determine when a triple peak occurs (the peaks of all three cycles occur in the same day) for any person. For each cycle, you will be given the number of days from the beginning of the current year at which one of its peaks (not necessarily the first) occurs. You will also be given a date expressed as the number of days from the beginning of the current year. You task is to determine the number of days from the given date to the next triple peak. The given date is not counted. For example, if the given date is 10 and the next triple peak occurs on day 12, the answer is 2, not 3. If a triple peak occurs on the given date, you should give the number of days to the next occurrence of a triple peak.
Input
Output
Case 1: the next triple peak occurs in 1234 days.
Use the plural form ``days'' even if the answer is 1.
Sample Input
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
Sample Output
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.
Source
East Central North America 1999
- 給出3個循環的週期,讓計算自給定date開始下一個巔峯的天數
- 想到中國剩餘定理CRT
- 解同餘方程組:
x = a[0] (mod 23); x = a[1] (mod 28); x = a[2] (mod 33);
然後直到找到第一個比date大的解,相減即可
#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <climits>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
typedef long long LL ;
typedef unsigned long long ULL ;
const int maxn = 1000 + 10 ;
const int inf = 0x3f3f3f3f ;
const int npos = -1 ;
const int mod = 1e9 + 7 ;
const int mxx = 100 + 5 ;
const double eps = 1e-6 ;
int extgcd(int a, int b, int &x, int &y){
if(0==b){
x=1;
y=0;
return a;
}else{
int r=extgcd(b,a%b,y,x);
y-=x*(a/b);
return r;
}
}
int crt(int a[], int m[], int n, int &M){
M=1;
for(int i=0;i<n;i++)
M*=m[i];
int res=0;
for(int i=0;i<n;i++){
int x, y;
int tm=M/m[i];
extgcd(tm,m[i],x,y);
res=(res + tm*x*a[i])%M;
}
return (res + M)%M;
}
int cal(int a[], int m[], int n, int D){
int M;
int res=crt(a,m,n,M);
while(res<=D)
res+=M;
return res-D;
}
int a[3], n=3, T=0, D;
int b[3]={23,28,33};
int main(){
// freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
while(~scanf("%d %d %d %d",&a[0],&a[1],&a[2],&D) && -1!=a[0]){
T++;
for(int i=0;i<n;i++)
a[i]=((a[i]%b[i]) + b[i])%b[i];
printf("Case %d: the next triple peak occurs in %d days.\n",
T,cal(a,b,n,D));
}
return 0;
}