結果

問題 No.187 中華風 (Hard)
ユーザー antaanta
提出日時 2015-04-20 00:21:34
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 247 ms / 3,000 ms
コード長 4,057 bytes
コンパイル時間 1,501 ms
コンパイル使用メモリ 99,636 KB
実行使用メモリ 20,356 KB
最終ジャッジ日時 2023-09-17 22:11:07
合計ジャッジ時間 5,212 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
4,376 KB
testcase_01 AC 10 ms
4,376 KB
testcase_02 AC 155 ms
14,256 KB
testcase_03 AC 157 ms
13,972 KB
testcase_04 AC 236 ms
19,488 KB
testcase_05 AC 246 ms
19,924 KB
testcase_06 AC 247 ms
20,356 KB
testcase_07 AC 237 ms
19,444 KB
testcase_08 AC 171 ms
11,132 KB
testcase_09 AC 172 ms
11,048 KB
testcase_10 AC 171 ms
11,056 KB
testcase_11 AC 242 ms
19,708 KB
testcase_12 AC 240 ms
19,448 KB
testcase_13 AC 11 ms
4,380 KB
testcase_14 AC 10 ms
4,380 KB
testcase_15 AC 146 ms
13,916 KB
testcase_16 AC 152 ms
13,856 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 8 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 183 ms
15,800 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 243 ms
19,772 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#include <bitset>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

vector<bool> isprime;
vector<int> primes;
void sieve(int n){
	if((int)isprime.size() >= n+1) return;
	isprime.assign(n+1, true);
	isprime[0] = isprime[1] = false;
	int sqrtn = (int)(sqrt(n * 1.) + .5);
	for(int i = 2; i <= sqrtn; i ++) if(isprime[i]) {
		for(int j = i * i; j <= n; j += i)
			isprime[j] = false;
	}
	primes.clear();
	for(int i = 2; i <= n; i ++) if(isprime[i])
		primes.push_back(i);
}

typedef int FactorsInt;
typedef vector<pair<FactorsInt,int> > Factors;
void primeFactors(FactorsInt x, Factors &out_v) {
	out_v.clear();
	int sqrtx = (int)(sqrt(x*1.) + 10.5);
	sieve(sqrtx);
	for(vector<int>::const_iterator p = primes.begin(); p != primes.end(); ++ p) {
		if(*p > sqrtx) break;
		if(x % *p == 0) {
			int t = 1;
			x /= *p;
			while(x % *p == 0) {
				t ++;
				x /= *p;
			}
			out_v.push_back(make_pair(*p, t));
		}
	}
	if(x != 1) out_v.push_back(make_pair(x, 1));
}

int inverse(signed a, const int MOD) {
	a %= MOD;
	if(a < 0) a += MOD;
	signed b = MOD, u = 1, v = 0;
	while(b) {
		signed t = a / b;
		a -= t * b; swap(a, b);
		u -= t * v; swap(u, v);
	}
	if(u < 0) u += MOD;
	return u;
}

int powpq(int p, int q) {
	int r = 1;
	rep(i, q) r *= p;
	return r;
}

int main() {
	sieve(31623);
	int N;
	scanf("%d", &N);
	Factors fs;
	map<int,pii> mods;
	bool ok = true;
	rep(i, N) {
		int X, Y;
		cin >> X >> Y;
		primeFactors(Y, fs);
		each(j, fs) {
			int p = j->first, q = j->second;
			int pq = powpq(p, q);
			pii &t = mods[p];
			if(t.first == 0) t.first = 1;
			int u = min(pq, t.first);
			ok &= t.second % u == X % u;
			if(t.first < pq) {
				t.first = pq;
				t.second = X % pq;
			}
		}
	}
	if(!ok) {
		puts("-1");
		return 0;
	}
	
	vector<int> m, x;
	each(i, mods) {
		m.push_back(i->second.first);
		x.push_back(i->second.second);
	}
	m.push_back(1000000007);
	int P = m.size();
//	rep(i, P-1) cerr << i << ": " << m[i] << ", " << x[i] << endl;
	vector<vi> prod(P, vi(P+1));
	vector<vi> table(P, vi(P+1));
	//table[i][j+1]
	//= CRT(x[0..j]) mod m[i]
	//  let b   = prod(m[0..j-1]) mod a2
	//      t   = inverse(b) mod a2
	//      a1  = CRT(m[0..j-1]) mod a2
	//      a1' = CRT(m[0..j-1]) mod m[i]
	//      h   = (x[j] - a1) * t mod a2
	//      n1  = prod(m[0..j-1]) mod m[i]
	//   in (a1' + n1 * h) mod m[i]
	//
	rep(i, P) {
		prod[i][0] = 1;
		rep(j, P)
			prod[i][j+1] = (ll)prod[i][j] * m[j] % m[i];
	}
	rep(i, P) {
		table[i][0] = 0;
		int b = 1;
		rep(j, i) {
			int b = prod[j][j];
			int t = inverse(b, m[j]);
			int a1 = table[j][j];
			int a1m = table[i][j];
			int h = (ll)(x[j] - a1) * t % m[j];
			if(h < 0) h += m[j];
			int n1 = prod[i][j];
			table[i][j+1] = (a1m + (ll)n1 * h) % m[i];
//			cerr << i << ", " << j << ": " << table[i][j+1] << endl;
		}
	}
	int ans = table[P-1][P-1];
	bool zero = true;
	each(i, mods)
		zero &= i->second.second == 0;
	if(zero) ans += prod[P-1][P-1];

	printf("%d\n", ans);
	return 0;
}
0