中國剩餘原理

傳送門:POJ 1006

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#define maxn  1010
typedef 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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章