UVA756 Biorhythms 中国剩余定理
//#pragma GCC optimize(2) #pragma comment(linker, "/STACK:10240000,10240000") #include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<string> #include<queue> #include<map> #include<set> #include<stack> #include<list> #include<ctime> #include<ctype.h> #include<stdlib.h> #include<bitset> #include<algorithm> #include<assert.h> #include<numeric> //accumulate #define endl "\n" #define fi first #define se second #define forn(i,s,t) for(int i=(s);i<(t);++i) #define mem(a,b) memset(a,b,sizeof(a)) #define rush() int MYTESTNUM;cin>>MYTESTNUM;while(MYTESTNUM--) #define debug(x) printf("%d\n",x) #define inf 0x3f3f3f3f #define INF 0x3f3f3f3f3f3f3f3f #define mp make_pair #define pb push_back #define sc(x) scanf("%d",&x) #define sc2(x,y) scanf("%d%d",&x,&y) #define sc3(x,y,z) scanf("%d%d%d",&x,&y,&z) #define pf(x) printf("%d\n",x) #define pf2(x,y) printf("%d %d\n",x,y) #define pf3(x,y,z) printf("%d %d %d\n",x,y,z) #define ll long long #define ull unsigned long long #define dd double #define pfs puts("*****") #define pfk(x) printf("%d ",(x)) #define kpf(x) printf(" %d",(x)) #define pfdd(x) printf("%.5f\n",(x)); #define pfhh printf("\n") using namespace std; const ll P=1e9; ll mul(ll a, ll b){ll ans = 0;for(;b;a=a*2%P,b>>=1) if(b&1) ans=(ans+a)%P;return ans;} ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;} ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;} const int maxn=550; inline int read() { int X=0,w=0; char ch=0; while(!isdigit(ch)) {w|=ch=='-';ch=getchar();} while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar(); return w?-X:X; } int gcd(int a,int b){ return !a?b:gcd(b%a,a); } /* POJ1006 UVA756 UVALive5421 Biorhythms */ // 递推法实现扩展欧几里德算法 long a[3], m[3]; long n=3; long exgcd(long a, long b, long *x, long *y) { long x0=1, y0=0, x1=0, y1=1; long r, q; *x=0; *y=1; r = a % b; q = (a - r) / b; while(r) { *x = x0 - q * x1; *y = y0 - q * y1; x0 = x1; y0 = y1; x1 = *x; y1 = *y; a = b; b = r; r = a % b; q = (a - r) / b; } return b; } // 扩展欧几里德算法求逆元 long minv(long a, long p) { long x, y; exgcd(a, p, &x, &y); return x<0 ? x+p : x; } // 中国剩余定理(Chinese remainder theorem, CRT) long crt() { long bm=1, mi, x=0; int i; for(i=0; i<n; i++) bm *= m[i]; for(i=0; i<n; i++) { mi = bm / m[i]; x += a[i] * mi * minv(mi, m[i]); x %= bm; } return x; } int main() { int kase=1; while(1) { int sum=0; for(int i=0;i<3;++i){ sc(a[i]); if(a[i]==-1) sum++; } int d; sc(d); if(sum==3)break; m[0]=23, m[1]=28, m[2]=33; long ans = crt(); while(ans<=d) ans+=21252; printf("Case %d: the next triple peak occurs in %ld days.\n",kase++,ans-d); } return 0; }