中国剩余定理

我的智商之低,简直举世无匹,旷烁古今,无与伦比.

在傻逼这件事上真是做到了极致.

不想说什么了.

传送门

//Achen
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<queue>
#include<cmath>
const int N=5;
typedef long long LL; 
using namespace std;
int d,m[N],b[N],M;
template<typename T> void read(T &x) {
    char ch=getchar(); x=0; T f=1;
    while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    if(ch=='-') f=-1,ch=getchar();
    for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f;
}

void exgcd(int a,int b,int &d,int &x,int &y) {
    if(!b) {d=a; x=1; y=0; return;}
    exgcd(b,a%b,d,y,x); y-=a/b*x;
}

int china(int n) {
    int res=0; M=1; 
    for(int i=1;i<=n;i++) M*=m[i];
    for(int i=1;i<=n;i++) {
        int tp=M/m[i],x,y,d;
        exgcd(m[i],tp,d,x,y);
        res=(res+y*tp*b[i]%M)%M;
    }
    res=(res-d+M)%M; if(res==0) res=M;
    return res;
}

int main() {
#ifdef DEBUG
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
#endif
    m[1]=23; m[2]=28; m[3]=33;
    for(int i=1;i;i++) {
        read(b[1]); read(b[2]); read(b[3]); read(d);
        if(b[1]==-1) break;
        printf("Case %d: the next triple peak occurs in %d days.
",i,china(3));
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/Achenchen/p/8098346.html