結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
4,380 KB
testcase_01 AC 37 ms
4,376 KB
testcase_02 AC 64 ms
4,380 KB
testcase_03 AC 62 ms
4,376 KB
testcase_04 AC 187 ms
4,380 KB
testcase_05 AC 179 ms
4,380 KB
testcase_06 AC 183 ms
4,376 KB
testcase_07 AC 187 ms
4,380 KB
testcase_08 AC 363 ms
4,376 KB
testcase_09 AC 362 ms
4,380 KB
testcase_10 AC 362 ms
4,376 KB
testcase_11 AC 183 ms
4,376 KB
testcase_12 AC 187 ms
4,376 KB
testcase_13 AC 272 ms
4,380 KB
testcase_14 AC 225 ms
4,380 KB
testcase_15 AC 105 ms
4,380 KB
testcase_16 AC 109 ms
4,380 KB
testcase_17 AC 2 ms
4,384 KB
testcase_18 AC 33 ms
4,380 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 156 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 185 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;

#define pb push_back
#define range(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n)  range(i,0,n)
#define dump(x)  cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl;

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

ll modinv(ll a,ll b){
	ll res,dummy;
	extgcd(a,b,res,dummy);
	if(res<0) res+=b, dummy-=a;
	return res;
}

const ll mod=1000000007;

ll chinese_remainder(vector<ll> b,vector<ll> m){
	int n=m.size();
	set<ll> P;
	rep(i,n){
		ll cur=m[i];
		for(ll f=2;f*f<=cur;++f){
			if(cur%f) continue;
			while(cur%f==0) cur/=f;
			P.insert(f);
		}
		if(cur>1) P.insert(cur);
	}

	vector<ll> factor(n);
	for(auto &prime:P){
		int index=0;
		rep(i,n){
			factor[i]=1LL;
			ll cur=m[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(b[index]%factor[i]!=b[i]%factor[i]) return -1;
			m[i]/=factor[i],b[i]%=m[i];
		}
	}

	vector<ll> constant(n,0LL),coef(n,1LL),v(n,0LL);
	rep(i,n){
		v[i]=(b[i]-constant[i]+m[i])%m[i];
		v[i]=(v[i]*modinv(coef[i],m[i]))%m[i];
		range(j,i+1,n){
			constant[j]=(constant[j]+v[i]*coef[j])%m[j];
			coef[j]=(coef[j]*m[i])%m[j];
		}
	}

	ll ans=0LL;
	for(int i=n-1;i>=0;--i) ans=(ans*m[i]+v[i])%mod;
	return ans;
}

int main(void){
	int N;
	cin >> N;
	vector<ll> X(N),Y(N);
	rep(i,N) cin >> X[i] >> Y[i];

	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]);
		ll ans=1LL;
		rep(i,N) ans=(ans*Y[i])%mod;
		cout << ans << endl;
	}else{
		cout << chinese_remainder(X,Y) << endl;
	}
	return 0;
}
0