#include #include using namespace std; #define endl "\r" #define int long long const int N = 10; int n = 3; int a[N] = {0, 23, 28, 33}; int m[N]; int A = 1; int x, y; int res; int exgcd(int a, int b, int &x, int &y) { if (!b) { x = 1, y = 0; return a; } int d = exgcd(b, a % b, y, x); y -= a / b * x; return d; } int qmul(int a, int b, int mod) { int res = 0; while (b) { if (b & 1) res = (res + a) % mod; a = (a + a) % mod; b >>= 1; } return res; } void CRT() { for (int i = 1; i <= n; i++) { m[i] = (m[i] % a[i] + a[i]) % a[i]; A *= a[i]; } for (int i = 1; i <= n; i++) { int Ai = A / a[i]; exgcd(Ai, a[i], x, y); x = (x % a[i] + a[i]) % a[i]; res = (res + qmul(m[i], A / a[i] * x, A)) % A; } } int T; int p, e, i, d; signed main() { while (cin >> p >> e >> i >> d && ~p) { T++; A = 1; res = 0; m[1] = p, m[2] = e, m[3] = i; CRT(); if (res <= d) res += A; printf("Case %lld: the next triple peak occurs in %lld days.\n", T, res - d); } }