B - Biorhythms——（中国剩余定理）

0
0
0
1. 云栖社区>
2. 博客>
3. 正文

B - Biorhythms——（中国剩余定理）

angel_imp 2016-03-22 21:31:00 浏览753

Description

Input

Output

Case 1: the next triple peak occurs in 1234 days.

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.```

Hint

Translator

`(n+d)%23=p;  `
`(n+d)%28=e;   `
`(n+d)%33=i;`

My Code：

```#include <iostream>
#include <cstdio>
using namespace std;
///(n+d)%23=p;   (n+d)%28=e;   (n+d)%33=i
void exgcd(int a, int b, int &x, int &y)
{
if(b == 0)
{
x = 1;
y = 0;
return;
}
int x1, y1;
exgcd(b, a%b, x1, y1);
x = y1;
y = x1 - (a/b)*y1;
}
int main()
{
int d, p, e, i, cas = 1;
while(cin>>p>>e>>i>>d)
{
if(p < 0)
break;
int m = 21252;
int M1 = 924;
int M2 = 759;
int M3 = 644;
int x1, x2, x3, y;
exgcd(M1, 23, x1, y);
x1 = (x1%23+23)%23;
exgcd(M2, 28, x2, y);
x2 = (x2%28+28)%28;
exgcd(M3, 33, x3, y);
x3 = (x3%33+33)%33;
int ans = ((p*M1*x1+e*M2*x2+i*M3*x3-d)%m+m)%m;
if(ans == 0)
ans = m;
printf("Case %d: the next triple peak occurs in %d days.\n",cas++,ans);
}
return 0;
}```

angel_imp
+ 关注