60pts求条
查看原帖
60pts求条
639198
Steve_xh楼主2024/5/12 09:38
#include<bits/stdc++.h>
#ifdef ONLINE_JUDGE
#define int __int128
#else
#define int long long
#endif
using namespace std;
int n;
class Faster_IO{
private:
	char buf[1<<20],*p1,*p2;
	char pbuf[1<<20],*pp;
	inline char gc()
	 {return ((p1==p2)?(p2=(p1=buf)+fread(buf,1,1<<20,stdin)):0),((p1==p2)?(EOF):(*p1++));}
	inline bool blank(char ch)
	 {return (ch==EOF||ch==' '||ch=='\0'||ch=='\n'||ch=='\r'||ch=='\t');}
	inline void push(const char &c)
	 {((pp-pbuf==(1<<20))?(fwrite(pbuf,1,1<<20,stdout),pp=pbuf):0),*pp++=c;}
public:
	Faster_IO():p1(buf),p2(buf),pp(pbuf){}
	~Faster_IO(){fwrite(pbuf,1,pp-pbuf,stdout);}
	template<class T> inline Faster_IO& read(T &x){
		bool s=false;x=0;char ch=gc();
		for(;!('0'<=ch&&ch<='9');ch=gc())(ch=='-')?(s=true):0;
		for(;('0'<=ch&&ch<='9');ch=gc())x=(x<<1)+(x<<3)+(ch^48);
		(s)?(x=-x):0;
		return *this;
	}
	inline Faster_IO& read(char *s){
		char ch=gc();
		for(;blank(ch);ch=gc());
		for(;!blank(ch);ch=gc())*s++=ch;
		*s='\0';
		return *this;
	}
	inline Faster_IO& read(char &c)
	 {for(c=gc();blank(c);c=gc());return *this;}
	template<class T> inline Faster_IO& write(T x){
		(x<0)?(x=-x,push('-')):(void)0;
		static T sta[35];
		size_t tp=0;
		do sta[tp++]=x%10,x/=10;while(x);
		while(tp)push(sta[--tp]|48);
		return *this;
	}
	inline Faster_IO& write(char *s,int length)
	 {for(;length--;)push(*s++);return *this;}
	inline Faster_IO& write(char *s)
	 {return write(s,strlen(s));}
	inline Faster_IO& write(const char *s)
	 {for(;s;)push(*s++);return *this;}
	template<class T> inline Faster_IO& write(T x,char lastChar)
	 {write(x),push(lastChar);return *this;}
	inline Faster_IO& write(char *s,int length,char lastChar)
	 {write(s,length),push(lastChar);return *this;}
}io;
struct LineConEqua{
	int a,m;	// x ≡a (mod n)
	LineConEqua(){}
	inline void input(){io.read(this->m).read(this->a);}
}e[100005];
int exgcd(int a,int b,int &x,int &y){
	if(!b){x=1,y=0;return a;}
	int t=exgcd(b,a%b,y,x);
	y-=x*a/b;
	return t;
}
int excrt(){
	LineConEqua now=e[1];
	for(int i=2;i<=n;i++){
		int a1=now.a,a2=e[i].a,m1=now.m,m2=e[i].m,p,q,gcd=exgcd(m1,m2,p,q);
		if(((a2-a1)%gcd+gcd)%gcd)
			return -1;
		now.a=m1*(p*((a2-a1)/gcd))+a1,now.m=m1*m2/gcd;
	}
	return 
	(
	now.a
	%now.m
	+now.m
	)
	%now.m;
}
signed main(){
	io.read(n);
	for(int i=1;i<=n;i++) 
		e[i].input();
	io.write(excrt());
	return 0;
}
2024/5/12 09:38
加载中...