这道题三大OJ都有,只不过输入输出有点不一样,我的输入输出是ZOJ版本。。。这题是中国剩余定理裸题,我用的是非互质情况下的中国剩余定理。。
#include <iostream> #include <sstream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <map> #include <set> #include <bitset> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <climits> #define maxn 800005 #define eps 1e-6 #define mod 10007 #define INF 99999999 #define lowbit(x) (x&(-x)) //#define lson o<<1, L, mid //#define rson o<<1 | 1, mid+1, R typedef long long LL; using namespace std; void extend_gcd(int a, int b, int &d, int &x, int &y) { if(b == 0) { d = a, x = 1, y = 0; } else { extend_gcd(b, a%b, d, y, x), y -= x*(a/b); } } int a[maxn], b[maxn]; int main(void) { int d, a1, b1, i, a2, b2, g, x, y, tmp, _ = 0, ans, __; scanf("%d", &__); while(__--){ _ = 0; while(scanf("%d%d%d%d", &b[0], &b[1], &b[2], &d), b[0]!=-1 && b[1]!=-1 && b[2]!=-1 && d!=-1) { a[0] = 23, a[1] = 28, a[2] = 33; for(i = 0; i < 3; i++) b[i]%=a[i]; a1 = a[0], b1 = b[0]; for(i = 1; i < 3; i++) { a2 = a[i], b2 = b[i]; extend_gcd(a1, a2, g, x, y); tmp = a2/g; x = x*(b2-b1)/g; x = (x%tmp+tmp)%tmp; b1 = a1*x+b1; a1 = (a1*a2)/g; b1 = (b1%a1+a1)%a1; } ans = ((b1-d)+a1)%a1; if(ans == 0) ans = a1; printf("Case %d: the next triple peak occurs in %d days.\n", ++_, ans); } if(__) printf("\n"); } return 0; }