pku1006 Biorhythms

http://poj.org/problem?id=1006

数论,中国剩余定理

 1 #include <stdio.h>
 2 
 3 int extend_gcd(int a, int b, int &x, int &y)
 4 {
 5     if(b == 0)
 6     {
 7         x = 1;
 8         y = 0;
 9         return a;
10     }
11     int r = extend_gcd(b, a%b, y, x);
12     y -= x*(a/b);
13     return r;
14 }
15 
16 int CRT(int a[], int m[], int n)
17 {
18     int M = 1;
19     for(int i=0; i<n; i++)
20     {
21         M *= m[i];
22     }
23     int ret = 0;
24     for(int i=0; i<n; i++)
25     {
26         int x, y;
27         int tm = M/m[i];
28         extend_gcd(tm, m[i], x, y);
29         ret = (ret + tm*x*a[i])%M;
30     }
31     return (ret+M)%M;
32 }
33 
34 int main()
35 {
36     int cases, d, i, r;
37     int a[3], m[3] = {23, 28, 33};
38     cases = 0;
39     while(scanf("%d%d%d%d", &a[0], &a[1], &a[2], &d), a[0]+1||a[1]+1||a[2]+1||d+1)
40     {
41         cases ++;
42         r = CRT(a, m, 3);
43         r -= d;
44         r += 21252;
45         r %= 21252;
46         if(r == 0)
47         {
48             r = 21252;
49         }
50         printf("Case %d: the next triple peak occurs in %d days.\n", cases, r);
51     }
52     return 0;
53 }

 

转载于:https://www.cnblogs.com/yuan1991/archive/2013/05/25/pku1006.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值