UVA756 Biorhythms Chinese remainder theorem

//#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 ;
     the while {W | = CH == (isdigit (CH)!) ' - ' ; CH = getchar ();}
     the 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 * / 
// recursive method to realize the extended Euclidean algorithm 
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; 
        yO = Y1; 
        X1 = * X; 
        Y1 = * Y; 

        A = B; 
        B = R & lt; 
        R & lt = A% B; 
        Q = (A - R & lt) / B; 
    } 
    return B; 
} 

// extension Euclidean algorithm inversing 
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;
}
View Code

 

Guess you like

Origin www.cnblogs.com/LS-Joze/p/11446363.html