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 }