結果

問題 No.187 中華風 (Hard)
ユーザー 🐬hec🐬hec
提出日時 2015-07-29 23:08:20
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,830 bytes
コンパイル時間 2,857 ms
コンパイル使用メモリ 155,488 KB
実行使用メモリ 4,656 KB
最終ジャッジ日時 2023-09-24 22:05:08
合計ジャッジ時間 3,750 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
4,380 KB
testcase_01 AC 6 ms
4,528 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 70 ms
4,384 KB
testcase_09 AC 69 ms
4,376 KB
testcase_10 AC 70 ms
4,428 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 6 ms
4,380 KB
testcase_14 AC 7 ms
4,384 KB
testcase_15 AC 109 ms
4,380 KB
testcase_16 AC 113 ms
4,380 KB
testcase_17 AC 5 ms
4,376 KB
testcase_18 AC 7 ms
4,516 KB
testcase_19 AC 5 ms
4,380 KB
testcase_20 WA -
testcase_21 AC 5 ms
4,376 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 5 ms
4,412 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 mod_inv(ll a,ll mod){
	ll n=mod-2,b=1LL;
	while(n){if(n&1)b=b*a%mod;a=a*a%mod;n>>=1;}
	return b;
}

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

const ll mod=1000000007;

const int nmax=1000010;
bool isprime[nmax];
vector<int> 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]){
			primes.pb(i);
			for(int j=i*i;j<nmax;j+=i)
				isprime[j]=false;
		}
}

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){
		int 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]=1;
			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(X[index]%factor[i]!=X[i]%factor[i]){
				cout << -1 << endl;
				return 0;
			}
			Y[i]/=factor[i];
			X[i]%=Y[i];
		}
	}


	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)
		if(Y[i]==1) continue;
		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];
		}
	}
	ll cur=1LL;
	rep(i,N){
		ans=(ans+v[i]*cur)%mod;
		cur=(cur*Y[i])%mod;
	}

	cout << ans << endl;
	return 0;
}
0