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

#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;
}

 

posted @ 2022-07-23 12:04  冬天丶的雨  阅读(33)  评论(0编辑  收藏  举报
Live2D