結果

問題 No.187 中華風 (Hard)
ユーザー sigma425sigma425
提出日時 2016-02-03 20:18:31
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 60 ms / 3,000 ms
コード長 1,655 bytes
コンパイル時間 1,620 ms
コンパイル使用メモリ 177,680 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-21 18:54:58
合計ジャッジ時間 3,581 ms
ジャッジサーバーID
(参考情報)
judge10 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
4,348 KB
testcase_01 AC 38 ms
4,348 KB
testcase_02 AC 53 ms
4,348 KB
testcase_03 AC 53 ms
4,348 KB
testcase_04 AC 59 ms
4,348 KB
testcase_05 AC 60 ms
4,348 KB
testcase_06 AC 60 ms
4,348 KB
testcase_07 AC 59 ms
4,348 KB
testcase_08 AC 49 ms
4,348 KB
testcase_09 AC 49 ms
4,348 KB
testcase_10 AC 49 ms
4,348 KB
testcase_11 AC 60 ms
4,348 KB
testcase_12 AC 59 ms
4,348 KB
testcase_13 AC 38 ms
4,348 KB
testcase_14 AC 38 ms
4,348 KB
testcase_15 AC 52 ms
4,348 KB
testcase_16 AC 52 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 33 ms
4,348 KB
testcase_19 AC 2 ms
4,348 KB
testcase_20 AC 50 ms
4,348 KB
testcase_21 AC 2 ms
4,348 KB
testcase_22 AC 60 ms
4,348 KB
testcase_23 AC 2 ms
4,348 KB
testcase_24 AC 2 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define rep1(i,n) for(int i=1;i<=(int)(n);i++)
#define all(c) c.begin(),c.end()
#define pb push_back
#define fs first
#define sc second
#define show(x) cout << #x << " = " << x << endl
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
using namespace std;
typedef long long ll;
const ll c=32000;
bool isp[c+1];
vector<ll> ps;
void makeprime(){
	for(int i=2;i<=c;i++) isp[i]=1;
	for(int i=2;i<=c;i++) if(isp[i]){
		for(int j=i*2;j<=c;j+=i) isp[j]=0;
		ps.pb(i);
	}
}
ll inv(ll a,ll m){
	ll b=m,u=1,v=0;
	while(b){
		ll t=a/b;
		swap(a-=t*b,b);
		swap(u-=t*v,v);
	}
	u%=m;
	if(u<0) u+=m;
	return u;
}
typedef pair<ll,ll> P;		//val,mod;
map<ll,vector<P> > mp;
vector<P> vp;
bool all0=1;
ll garner(vector<P> rm,ll mod){		//remainder,mod
	rm.pb(P(0,mod));
	int N=rm.size();
	vector<ll> as(N,1);		//coefficients
	vector<ll> bs(N,0);		//constants
	rep(i,N-1){
		ll v=(rm[i].fs-bs[i])*inv(as[i],rm[i].sc)%rm[i].sc;
		if(v<0) v+=rm[i].sc;
		for(int j=i+1;j<N;j++){
			(bs[j]+=as[j]*v)%=rm[j].sc;
			(as[j]*=rm[i].sc)%=rm[j].sc;
		}
	}
	if(all0) return as[N-1];
	return bs[N-1];
}
int main(){
	makeprime();
	int N;
	cin>>N;
	rep(i,N){
		ll x,m;
		cin>>x>>m;
		if(x) all0=0;
		for(int p:ps){
			if(m%p==0){
				int k=0;
				int a=1;
				while(m%p==0) m/=p,a*=p,k++;
				mp[p].pb(P(x%a,a));
			}
		}
		if(m>1){
			mp[m].pb(P(x%m,m));
		}
	}
	for(auto it:mp){
		vector<P>& vc=it.sc;
		ll q=0,x=0;
		for(P p:vc){
			if(q<p.sc){
				q=p.sc;
				x=p.fs;
			}
		}
		for(P p:vc){
			if(x%p.sc!=p.fs){
				puts("-1");
				return 0;
			}
		}
		vp.pb(P(x,q));
	}
	cout<<garner(vp,1e9+7)<<endl;
}
0