一道中国剩余定理的例题
给你n对数,输入x,y就是%x意义下余y
#include<cstdio> #include<algorithm> #include<iostream> #include<cmath> typedef long long ll; using namespace std; ll ex_gcd(ll a,ll b,ll &x,ll &y) { if (!b) { x=1,y=0; return a; } ll fzy=ex_gcd(b,a%b,x,y); ll t=x; x=y;y=t-a/b*y; return fzy; } int main() { int t; ll z1,z2,z3,z4; while (cin>>t) { bool flag=0; scanf("%lld%lld",&z1,&z2); for (int i=1;i<t;i++) { scanf("%lld%lld",&z3,&z4); if (flag) continue; ll a=z1,b=z3,c=z4-z2; ll x,y; ll d=ex_gcd(a,b,x,y); if (c%d!=0) { flag=1; continue; } ll t=b/d; x=(x*(c/d)%t+t)%t; z2=z1*x+z2; z1=z1*(z3/d); } if (flag==1) cout<<-1<<endl; else cout<<z2<<endl; } }