[模板]中国剩余定理(曹冲养猪)
#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; }
本文来自博客园,作者:冬天丶的雨,转载请注明原文链接:https://www.cnblogs.com/WintersRain/p/16218068.html
为了一切不改变的理想,为了改变不理想的一切