其他分享
首页 > 其他分享> > [模板]中国剩余定理(曹冲养猪)

[模板]中国剩余定理(曹冲养猪)

作者:互联网

#include<cstdio>
#include<string>
#include<cstring>
#define WR WinterRain
using namespace std;
const long long WR=1001000;
struct pig{
    long long a,b;
}p[WR];
long long n,ans,sum=1;
long long read(){
    long long s=0,w=1;
    char ch=getchar();
    while(ch>'9'||ch<'0'){
        if(ch=='-') w=-1;
        ch=getchar();
    }
    while(ch<='9'&&ch>='0'){
        s=(s<<3)+(s<<1)+ch-'0';
        ch=getchar();
    }
    return s*w;
}
long long gcd(long long x,long long y){
    if(!y) return x;
    return gcd(y,x%y);
}
int main(){
    n=read();
    for(long long i=1;i<=n;i++){
        p[i].a=read(),p[i].b=read();
    }
    ans=p[1].b;
    for(long long i=1;i<n;i++){
        sum=sum*p[i].a/gcd(sum,p[i].a);
        while(ans%p[i+1].a!=p[i+1].b){
            ans+=sum;
        }
    }
    printf("%lld",ans);
    return 0;
}

 

标签:ch,long,pig,char,include,WR,养猪,曹冲,模板
来源: https://www.cnblogs.com/WintersRain/p/16218068.html