結果

問題 No.187 中華風 (Hard)
ユーザー 🐬hec🐬hec
提出日時 2015-07-30 01:42:32
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 115 ms / 3,000 ms
コード長 3,181 bytes
コンパイル時間 1,479 ms
コンパイル使用メモリ 162,140 KB
実行使用メモリ 5,044 KB
最終ジャッジ日時 2023-09-24 22:09:10
合計ジャッジ時間 3,632 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 14 ms
4,884 KB
testcase_01 AC 13 ms
4,872 KB
testcase_02 AC 38 ms
4,748 KB
testcase_03 AC 37 ms
4,764 KB
testcase_04 AC 55 ms
4,980 KB
testcase_05 AC 54 ms
5,044 KB
testcase_06 AC 54 ms
4,980 KB
testcase_07 AC 55 ms
4,752 KB
testcase_08 AC 67 ms
4,764 KB
testcase_09 AC 66 ms
4,800 KB
testcase_10 AC 67 ms
4,912 KB
testcase_11 AC 54 ms
4,752 KB
testcase_12 AC 55 ms
4,896 KB
testcase_13 AC 37 ms
4,768 KB
testcase_14 AC 33 ms
4,976 KB
testcase_15 AC 111 ms
4,748 KB
testcase_16 AC 115 ms
4,748 KB
testcase_17 AC 7 ms
4,968 KB
testcase_18 AC 13 ms
4,940 KB
testcase_19 AC 7 ms
4,900 KB
testcase_20 AC 45 ms
4,996 KB
testcase_21 AC 7 ms
4,768 KB
testcase_22 AC 55 ms
4,996 KB
testcase_23 AC 8 ms
4,868 KB
testcase_24 AC 7 ms
5,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
template<class T> inline T sqr(T x) {return x*x;}

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;

#define all(a)  (a).begin(),(a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define pb push_back
#define mp make_pair
#define each(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define exist(s,e) ((s).find(e)!=(s).end())
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define clr(a,b) memset((a), (b) ,sizeof(a))
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi  = acos(-1.0);
const ll INF =1LL << 62;
const int inf =1 << 29;

ll ext_gcd(ll a,ll b,ll& x, ll& y) {
	if(b==0) return x=1,y=0,a;
	ll g=ext_gcd(b,a%b,y,x);
	y-=a/b*x;
	return g;
}

ll mod_inv(ll a,ll b){
	ll res,dummy,g=ext_gcd(a,b,res,dummy);
	if(res<0) res+=b, dummy-=a;
	return res;
}

ll X[1010],Y[1010],factor[1010];

const ll mod=1000000007;

const int nmax=1000010;
bool isprime[nmax];
vector<ll> primes;

void init(){
	for(int i=0;i<nmax;i++) isprime[i]=true;
	isprime[0]=isprime[1]=false;
	for(int i=2;i*i<nmax;i++){
		if(isprime[i]){
			for(int j=i*i;j<nmax;j+=i)
				isprime[j]=false;
		}
	}
	for(int i=2;i<nmax;i++) if(isprime[i]) primes.pb(i);
}

int main(void){
	init();
	int N;
	cin >> N;
	rep(i,N) cin >> X[i] >> Y[i];
	ll ans=0LL;

	int zero=0;
	rep(i,N) zero+=(X[i]==0);

	if(zero==N){
		rep(i,N)rep(j,i) Y[i]/=__gcd(Y[i],Y[j]);
		ans=1LL;
		rep(i,N) ans=(ans*Y[i])%mod;
		cout << ans << endl;
		return 0;
	}

	set<ll> P;
	rep(i,N){
		ll cur=Y[i];
		for(auto &f:primes){
			if(f*f>cur) break;
			ll num=1LL;
			while(cur%f==0) num*=f,cur/=f;
			if(num==1LL) continue;
			P.insert(f);
		}
		if(cur>1) P.insert(cur);
	}

	for(auto &prime:P){
		int index=0;
		rep(i,N){
			factor[i]=1LL;
			ll cur=Y[i];
			while(cur%prime==0) factor[i]*=prime,cur/=prime;
			if(factor[i]>factor[index]) index=i;
		}
		rep(i,N)if(i!=index){
			if(factor[i]==1) continue;
			if(X[index]%factor[i]!=X[i]%factor[i]){
				cout << -1 << endl;
				return 0;
			}
			Y[i]/=factor[i];
			X[i]%=Y[i];
		}
	}

	vector<pll> rearrange;
	rep(i,N) if(Y[i]>1LL) rearrange.pb({Y[i],X[i]});
	sort(begin(rearrange),end(rearrange));
	rep(i,rearrange.size()) X[i]=rearrange[i].second,Y[i]=rearrange[i].first;
	N=rearrange.size();

	vector<ll> constant(N,0LL);
	vector<ll> coef(N,1LL);
	vector<ll> v(N,0LL);

	rep(i,N){
		// constant = sigma (res*mod)
		// coef =  pi(mod)
		// constant+coef*v == X
		// coef*v == (X-constant)
		// v = coef^-1*(X-constant)
		v[i]=(X[i]-constant[i]+Y[i])%Y[i];
		v[i]=(v[i]*mod_inv(coef[i],Y[i]))%Y[i];
		range(j,i+1,N){
			constant[j]=(constant[j]+v[i]*coef[j])%Y[j];
			coef[j]=(coef[j]*Y[i])%Y[j];
		}
	}

	for(int i=N-1;i>=0;--i) ans=(ans*Y[i]+v[i])%mod;
	cout << ans << endl;
	return 0;
}
0