中国剩余定理——POJ-1006

题目链接

由题意得

ans+d≡p mod23

ans+d≡e mod28

ans+d≡i mod33

然后套模板就可以过了

题目代码

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
typedef long long LL;
const int maxn=1e5+100;
const int mod=21252;
LL a[maxn],m[maxn];
LL exgcd(LL a,LL b,LL &x,LL &y){
    if(b==0){
        x=1;
        y=0;
        return a;
    }
    LL d=exgcd(b,a%b,x,y);
    LL t=y;
    y=x-(a/b)*y;
    x=t;
    return d;
}
LL excrt(int n){
    LL x,y,ans=a[1],M=m[1];
    for(int i=2;i<=n;i++){
        LL c=(a[i]-ans%m[i]+m[i])%m[i];
        LL d=exgcd(M,m[i],x,y);
        if(c%d!=0)return -1;
        x=c/d*x%m[i];
        ans+=x*M;
        M=M/d*m[i];
        ans=(ans%M+M)%M;
    }
    return ans;
}
int main(){
    int ce=1;
    LL d;
    while(scanf("%lld%lld%lld%lld",&a[1],&a[2],&a[3],&d)){
        if(a[1]==-1&&a[2]==-1&&a[3]==-1&&d==-1)break;
        m[1]=23,m[2]=28,m[3]=33;
        LL x=excrt(3);
        x=(x-d+mod)%mod;
        if(!x)x=mod;
        printf("Case %d: the next triple peak occurs in %lld days.\n",ce++,x);
    }
    return 0;
}

 

posted @ 2019-08-14 16:34  helman78  阅读(134)  评论(0编辑  收藏  举报