結果

問題 No.187 中華風 (Hard)
ユーザー 👑 kmjpkmjp
提出日時 2015-04-20 02:14:56
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,220 bytes
コンパイル時間 1,682 ms
コンパイル使用メモリ 163,288 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 22:34:48
合計ジャッジ時間 9,104 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
4,380 KB
testcase_01 AC 37 ms
4,380 KB
testcase_02 AC 219 ms
4,380 KB
testcase_03 AC 214 ms
4,380 KB
testcase_04 AC 411 ms
4,376 KB
testcase_05 AC 405 ms
4,376 KB
testcase_06 AC 408 ms
4,376 KB
testcase_07 AC 410 ms
4,376 KB
testcase_08 AC 488 ms
4,376 KB
testcase_09 AC 489 ms
4,376 KB
testcase_10 AC 488 ms
4,380 KB
testcase_11 AC 409 ms
4,380 KB
testcase_12 AC 412 ms
4,380 KB
testcase_13 AC 254 ms
4,380 KB
testcase_14 AC 210 ms
4,376 KB
testcase_15 AC 314 ms
4,376 KB
testcase_16 AC 327 ms
4,380 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 32 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 327 ms
4,376 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 410 ms
4,376 KB
testcase_23 WA -
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<to;x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

set<ll> enumpr(ll n) {
	set<ll> V;
	for(ll i=2;i*i<=n;i++) while(n%i==0) V.insert(i),n/=i;
	if(n>1) V.insert(n);
	return V;
}

ll ext_gcd(ll p,ll q,ll& x, ll& y) { // get px+qy=gcd(p,q)
	if(q==0) return x=1,y=0,p;
	ll g=ext_gcd(q,p%q,y,x);
	y-=p/q*x;
	return g;
}

ll inv(ll p,ll q) { // return (1/p)%q ( p,q is co-prime)
	ll xx,yy,g=ext_gcd(p,q,xx,yy);
	if(xx<0) xx+=q, yy-=p;
	return xx;
}

ll CRT_garner(vector<pair<ll,ll> > V, ll mo=1000000007) {
	int i,j,k,l,r,x,y; string s;
	int N=V.size();
	set<ll> P;
	vector<int> num;
	
	FOR(i,N) {
		set<ll> S=enumpr(V[i].second);
		FORR(r,S) P.insert(r);
	}
	
	num=vector<int>(N,0);
	
	FORR(r,P) {
		y=0;
		FOR(x,N) {
			num[x]=1;
			j=V[x].second;
			while(j%r==0) j/=r, num[x]*=r;
			if(num[y]<num[x]) y=x;
		}
		FOR(x,N) if(x!=y) {
			if(V[x].first%num[x]!=V[y].first%num[x]) return -1;
			V[x].second /= num[x];
			V[x].first %= V[x].second;
		}
	}
	vector<pair<ll,ll> > V2;
	FOR(x,N) if(V[x].second>1) V2.emplace_back(V[x].second,V[x].first);
	sort(V2.begin(),V2.end());
	V=V2;
	N=V.size();
	
	// garner
	FOR(y,N) FOR(x,y) {
		ll k=V[y].second-V[x].second;
		if(k<0) k+=V[y].first;
		V[y].second = k*inv(V[x].first,V[y].first)%V[y].first;
	}
	ll ret=0;
	for(x=N-1;x>=0;x--) ret = (ret*V[x].first+V[x].second)%mo;
	return ret;
}

void solve() {
	
	int N,i,x,y;
	vector<pair<ll,ll> > V;
	
	cin>>N;
	FOR(i,N) {
		cin>>x>>y;
		V.emplace_back(x,y);
	}
	ll ret=CRT_garner(V);
	if(ret==0) {
		FOR(y,N) FOR(x,y) V[y].second/=__gcd(V[x].second,V[y].second);
		ret=1;
		FOR(x,N) ret=ret*V[x].second%1000000007;
	}
	cout<<ret<<endl;
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false);
	FOR(i,argc-1) s+=argv[i+1],s+='\n';
	FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	solve(); return 0;
}
0