中国剩余定理poj1006
题意就是给出23,28,33三个周期,输入三个余数和起始时间,问下一个共同周期。
中国剩余定理模板题
中国剩余定理:
mi=n1*n2*...*n(i-1)*n(i+1)*...*ni
ci=mi ( mi^-1 mod ni )
a≡(a1c1+a2c2+...+akck)(mod n)
#include<iostream> #include<stdio.h> using namespace std; #define ll long long long long extend_gcd(long long a,long long b,long long &x,long long&y) { if(a==0&&b==0) return -1;//无最大公约数 if(b==0) { x=1; y=0; return a; } long long d=extend_gcd(b,a%b,y,x); y-=a/b*x; return d; } //ax = 1(mod n) long long mod_reverse(long long a,long long n) { long long x,y; long long d=extend_gcd(a,n,x,y); if(d==1) return (x%n+n)%n; else return -1; } int main() { ll p,e,i,dd,n1,n2,n3,m1,m2,m3; n1=23,n2=28,n3=33; m1=n2*n3; m2=n1*n3; m3=n1*n2; ll n = n1*n2*n3; ll m11=mod_reverse(m1,23); ll m22=mod_reverse(m2,28); ll m33=mod_reverse(m3,33); ll c1=m1*m11; ll c2=m2*m22; ll c3=m3*m33; int acm=1; while(~scanf("%lld%lld%lld%lld",&p,&e,&i,&dd)) { if(p==-1&&e==-1&&i==-1&&dd==-1) { break; } ll ans=(p*c1+e*c2+i*c3)%n; dd%=21252; ans=(ans-dd+21252)%21252; if(ans==0)ans=21252; printf("Case %d: the next triple peak occurs in %lld days.\n",acm++,ans); } return 0; }