中国剩余原理

来源:互联网 发布:舆情调查软件 编辑:程序博客网 时间:2024/06/05 22:07

传送门:POJ 1006

介绍一篇大佬的博客:http://972169909-qq-com.iteye.com/blog/1125532

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#define maxn  1010typedef long long LL;using namespace std;int extend_gcd(int a,int b,int &x,int &y);int main() {    //freopen("in.txt","r",stdin);    int M=21252,a[maxn],d,kase=0;    int m[4]={0,23,28,33};    while(scanf("%d %d %d %d",&a[1],&a[2],&a[3],&d)!=EOF){        if(a[1]==-1&&a[2]==-1&&a[3]==-1&&d==-1)break;        kase++;                int x,y,i,res=0;        for(i=1;i<=3;i++){            extend_gcd(M/m[i],m[i],x,y);            res=(res+a[i]*M/m[i]*x)%M;        }        if(res<=0)res+=M;        while(res<=d)res+=M;        printf("Case %d: ",kase);        printf("the next triple peak occurs in %d days.\n",res-d);    }    return 0;}int extend_gcd(int a,int b,int &x,int &y){    if(b==0){        x=1;        y=0;        return a;    }    int d=extend_gcd(b,a%b,y,x);    y=y-a/b*x;    return d;}