Основное
 
 
Отправитель Задача Язык Вердикт Время Память Отослано Протест.  
237021037 Дорешивание:
Schucking_Sattin
571E - 40 C++14 (GCC 6-32) Полное решение 15 мс 184 КБ 2023-12-13 13:57:10 2023-12-13 13:57:10
→ Исходный код
// Sea, You & Me
#include<bits/stdc++.h>
#define LL long long
#define DB double
#define MOD 1000000007
#define ls(x) (x << 1)
#define rs(x) (x << 1 | 1)
#define lowbit(x) ((-x) & x)
#define MP make_pair
#define MT make_tuple
#define VI vector<int>
#define VL vector<LL>
#define VII VI::iterator
#define VLI VL::iterator
#define all(x) x.begin(), x.end()
#define EB emplace_back
#define PII pair<int, int>
#define PIL pair<int, LL>
#define SI set<int>
#define SII SI::iterator
#define fi first
#define se second
using namespace std;
template<typename T> void chkmn(T &a, const T b) { (a > b) && (a = b); }
template<typename T> void chkmx(T &a, const T b) { (a < b) && (a = b); }
void Inc(int &a, const int &b) { ((a += b) >= MOD) && (a -= MOD); }
void Dec(int &a, const int &b) { ((a -= b) < 0) && (a += MOD); }
void Mul(int &a, const int &b) { a = 1LL * a * b % MOD; }
void Sqr(int &a) { a = 1LL * a * a % MOD; }
int inc(const int &a, const int &b) { return (a + b >= MOD) ? a + b - MOD : a + b; }
int dec(const int &a, const int &b) { return (a - b < 0) ? a - b + MOD : a - b; }
int mul(const int &a, const int &b) { return 1LL * a * b % MOD; }
int sqr(const int &a) { return 1LL * a * a % MOD; }
int qwqmi(int x, LL k = MOD - 2)
{
	int res = 1;
	while(k)
	{
		if(k & 1) Mul(res, x);
		k /= 2, Sqr(x);
	}
	return res;
}
template<typename T> void read(T &x)
{
	x = 0;
	int f = 1;
	char ch = getchar();
	while(!isdigit(ch))
	{
		if(ch == '-')
			f = -1;
		ch = getchar();
	}
	while(isdigit(ch))
	{
		x = (x << 1) + (x << 3) + (ch ^ 48);
		ch = getchar();
	}
	x = x * f;
}
#define NA puts("-1"), exit(0)
const int N = 105;
int n;
struct Primeint
{
	vector<PIL> vec;
	#define sz(A) (int)A.vec.size() 
	void Read()
	{
		int x; read(x);
		for(int i = 2; i * i <= x; ++i)
			if(x % i == 0)
			{
				int s = 0;
				while(x % i == 0)
					x /= i, ++s;
				vec.EB(MP(i, s));
			}
		if(x > 1) vec.EB(MP(x, 1));
	}
	void Print()
	{
		int res = 1;
		for(int i = 0; i < (int)vec.size(); ++i)
			Mul(res, qwqmi(vec[i].fi, vec[i].se));
		printf("%d\n", res);
		return;
	}
	friend Primeint operator * (Primeint A, Primeint B)
	{
		Primeint res; res.vec.clear();
		int i = 0, j = 0;
		while(i < sz(A) && j < sz(B))
		{
			if(A.vec[i].fi == B.vec[j].fi)
				res.vec.EB(MP(A.vec[i].fi, A.vec[i].se + B.vec[j].se)), ++i, ++j;
			else if(A.vec[i].fi < B.vec[j].fi)
				res.vec.EB(A.vec[i]), ++i;
			else res.vec.EB(B.vec[j]), ++j;
		}
		while(i < sz(A)) res.vec.EB(A.vec[i]), ++i;
		while(j < sz(B)) res.vec.EB(B.vec[j]), ++j;
		return res;
	}
	friend bool operator % (Primeint A, Primeint B) // whether A % B != 0
	{
		for(int i = 0, j = 0; j < sz(B); ++j)
		{
			while(i < sz(A) && A.vec[i].fi != B.vec[j].fi) ++i;
			if(i == sz(A) || A.vec[i].se < B.vec[j].se) return 1;
		}
		return 0;
	}
	friend Primeint operator / (Primeint A, Primeint B) // need A % B == 0
	{
		Primeint res; res.vec.clear();
		for(int i = 0, j = 0; i < sz(A); ++i)
		{
			if(j < sz(B) && A.vec[i].fi == B.vec[j].fi)
			{
				res.vec.EB(MP(A.vec[i].fi, A.vec[i].se - B.vec[j].se)), ++j;
				if(res.vec.back().se == 0) res.vec.pop_back();
			}
			else res.vec.EB(A.vec[i]);
		}
		return res;
	}
	friend Primeint operator & (Primeint A, Primeint B) // normalizing A - B
	{
		Primeint res; res.vec.clear();
		for(int i = 0, j = 0; i < sz(A); ++i)
		{
			if(j < sz(B) && A.vec[i].fi == B.vec[j].fi)
				res.vec.EB(MP(A.vec[i].fi, A.vec[i].se - B.vec[j].se)), ++j;
			else res.vec.EB(A.vec[i]);
		}
		return res;
	}
	friend bool operator | (Primeint A, Primeint B) // whether A = B^K
	{
		if(sz(A) == 0) return 1; // K = 0
		LL K = 7210721;
		for(int i = 0, j = 0; i <= sz(A); ++i, ++j)
		{
			while(j < sz(B) && B.vec[j].se == 0) ++j;
			if(i == sz(A))
			{
				if(j == sz(B)) return 1;
				return 0;
			}
			if(j == sz(B)) return 0;
			if(A.vec[i].fi != B.vec[j].fi || A.vec[i].se % B.vec[j].se) return 0;
			if(i == 0) K = A.vec[i].se / B.vec[j].se;
			if(A.vec[i].se / B.vec[j].se != K) return 0;
		}
		return 1;
	}
	friend Primeint operator ^ (Primeint A, LL B)
	{
		Primeint res; res.vec.clear();
		for(int i = 0; i < sz(A); ++i)
			res.vec.EB(MP(A.vec[i].fi, A.vec[i].se * B));
		return res;
	}
	friend Primeint operator + (Primeint A, Primeint B)
	{
		Primeint res; res.vec.clear();
		for(int i = 0; i < sz(A); ++i)
			res.vec.EB(MP(A.vec[i].fi, A.vec[i].se * B.vec[i].se / __gcd(A.vec[i].se, B.vec[i].se)));
		return res;
	}
}A, B, a[N], b[N], c[N];
struct Equation
{
	LL u, v, w; // ux + vy = w, gcd(u, v) = 1
	Equation(const LL U = 0, const LL V = 0, const LL W = 0){
		u = U, v = V, w = W;
	}
	friend bool operator == (Equation A, Equation B){
		return A.u == B.u && A.v == B.v && A.w == B.w;
	}
	void Print()
	{
		cerr << u << ' ' << v << ' ' << w << '\n';
	}
};
LL solve(Equation A, Equation B)
{
	LL k = A.u * B.v - A.v * B.u;
	LL b = A.w * B.v - A.v * B.w;
	if(!k || b % k || b / k < 0) NA;
	return b / k;
}
bool check(Primeint A)
{
	for(int i = 1; i <= n; ++i)
		if((A % a[i]) == 1 || ((A / a[i]) | b[i]) == 0) return 0;
	return 1;
}
LL EXGCD(LL &x, LL &y, LL a, LL b)
{
	if(!b) return x = 1, y = 0, a;
	int d = EXGCD(x, y, b, a % b), z = x;
	return x = y, y = z - (a / b) * y, d;
}
bool merge(int o)
{
	vector<Equation> E; E.clear();
	for(int i = 0; i < (int)a[o].vec.size(); ++i)
	{
		LL k1 = B.vec[i].se, b1 = A.vec[i].se;
		LL k2 = b[o].vec[i].se, b2 = a[o].vec[i].se;
//		cerr << k1 << ' ' << b1 << ' ' << k2 << ' ' << b2 << '\n';
		if(!k1 && !k2)
		{
			if(b1 != b2) NA;
			continue;
		}
		if(!k1)
		{
			if(b1 < b2 || (b1 - b2) % k2) NA;
			return A = A * (B ^ ((b1 - b2) / k2)), 0; 
		}
		if(!k2)
		{
			if(b2 < b1 || (b2 - b1) % k1) NA;
			return A = A * (B ^ ((b2 - b1) / k1)), 0; 
		}
		LL d = __gcd(k1, k2), g = b2 - b1;
		if(g % d) NA;
		k1 /= d, k2 /= d, g /= d;
		Equation now = Equation(k1, k2, g);
//		now.Print();
		if(!E.empty())
		{
			if(E[0] == now) continue;
			return A = A * (B ^ solve(E[0], now)), 0;
		}
		else E.EB(now);
	}
	if(!E.empty())
	{
		LL u = E[0].u, v = E[0].v, w = E[0].w, x, y;
		EXGCD(x, y, u, v);
		w = (w % v + v) % v;
		x = (x * w % v + v) % v;
		A = A * (B ^ x);
		B = B + b[o];
	}
	return 1;
}
int main()
{
	read(n);
	for(int i = 1; i <= n; ++i)
	{
		a[i].Read();
		b[i].Read();
		c[i] = a[i] * b[i];
	}
	
	// special judge 1
	for(int i = 1; i <= n; ++i)
		if(check(a[i]))
			return a[i].Print(), 0;
	
	// special judge 2 & normalization
	for(int i = 1; i <= n; ++i)
	{
		if(c[i].vec.size() != c[1].vec.size()) NA;
		for(int j = 0; j < (int)c[i].vec.size(); ++j)
			if(c[i].vec[j].fi != c[1].vec[j].fi) NA;
		a[i] = c[i] & b[i], b[i] = c[i] & a[i];
	}
	
	// merge a \times b^{k}
	A = a[1], B = b[1];
	for(int i = 2; i <= n; ++i)
		if(!merge(i))
		{
			if(check(A) == 0) NA;
			else return A.Print(), 0;
		}
	A.Print();
	return 0;
}

/* WA on test 5
2
1 6
2 6
ans : -1
myout : 2
*/

/* WA on test 20
14
1 2
2 4
8 16
128 256
32768 65536
4 8
256 512
16 32
64 128
1024 2048
4096 8192
65536 131072
262144 524288
4194304 8388608
ans : 846526526
myout : 286517992
*/

/* debug
1. 0 <-> 1
2. E.EB(now);
3. qwqmi(..., LL k)
*/

?
Время: ? ms, память: ? КБ
Вердикт: ?
Ввод
?
Вывод участника
?
Ответ жюри
?
Комментарий чекера
?
Диагностика
?
Показать детали тестирования