2019暑假牛客第10场-D,Han Xin and His Troops-拓展欧几里得-线性同余方程组

题目传送门
题面:
2019暑假牛客第10场-D,Han Xin and His Troops-拓展欧几里得-线性同余方程组_第1张图片
2019暑假牛客第10场-D,Han Xin and His Troops-拓展欧几里得-线性同余方程组_第2张图片
2019暑假牛客第10场-D,Han Xin and His Troops-拓展欧几里得-线性同余方程组_第3张图片
2019暑假牛客第10场-D,Han Xin and His Troops-拓展欧几里得-线性同余方程组_第4张图片
思路:
拓展欧几里得解线性同余方程组模板题。
只是要注意会爆long long,所以全部改为__int128输入输出。

#include
using namespace std;
#define per(i,a,b) for(int i=(a);i<=(b);i++)
#define rep(i,a,b) for(int i=(a);i>=(b);i--)
//------------------------------------------Big integer I/O
void scan(__int128 &x){//input
	x = 0;
	int f = 1;
	char ch;
	if((ch = getchar()) == '-'){
		f = -1;
	}else{
		x = x * 10 + (ch - '0');
	}
	while((ch = getchar()) >= '0' && ch <= '9'){
		x = x * 10 + (ch - '0');
	}
	x *= f;
}
void print(__int128 x){//output 
	if(x < 0){
		x = -x;
		putchar('-');
	}
	if(x > 9){
		print(x/10);//recursion
	}
	putchar(x%10+'0');
}
//-----------------------------------------
__int128 x = 0,y = 0,m = 0,n = 0,l = 0,k1 = 0,k2 = 0;
__int128 gcd(__int128 a,__int128 b){
	return b == 0 ? a : gcd(b,a % b); 
}
__int128 ex_gcd(__int128 a,__int128 b,__int128 &x,__int128 &y){
	if(b == 0){
		x = 1;y = 0;
		return a;
	}else{
		__int128 r = ex_gcd(b,a%b,x,y);
		__int128 tmp = x;
		x = y;y = tmp - (a/b)*y;
		return r;
	}
}
int main(){
	__int128 n,m;
	__int128 a1,r1,a2,r2;
	__int128 x0,y0,a,b,c;
	scan(n); scan(m);
	bool flag = true,fg0 = true;
	scan(a1); scan(r1);
	if(a1 != r1){
  		fg0 = false;
  	}
  	for(__int128 i = 1;i < n;++i){
  		scan(a2); scan(r2);
  		if(a2 != r2){
	  		fg0 = false;
	  	}
	  	a = a1; b = a2;c = r2 - r1;
	  	__int128 d = ex_gcd(a,b,x0,y0);
	  	if(c % d != 0){
	  		flag = false;
	  	}
	  	__int128 t = b/d;
	  	x0 = (x0*(c/d)%t + t) % t;
	  	r1 = a1*x0 + r1;
	  	a1 = a1*(a2 / d);
  	}
  	if(fg0){//特判0的情况
    	puts("0");
    	return 0;
    }
    if(!flag){
        puts("he was definitely lying");
        return 0;
    }
    if(r1 > m){
        puts("he was probably lying");
        return 0;
    }
    print(r1);
    puts("");

	return 0;
} 

你可能感兴趣的:(数论)