結果
| 問題 |
No.187 中華風 (Hard)
|
| コンテスト | |
| ユーザー |
kmjp
|
| 提出日時 | 2015-04-20 02:17:49 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 449 ms / 3,000 ms |
| コード長 | 2,262 bytes |
| コンパイル時間 | 1,740 ms |
| コンパイル使用メモリ | 177,152 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-07-04 16:14:16 |
| 合計ジャッジ時間 | 7,512 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 25 |
ソースコード
#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,nz=0;
vector<pair<ll,ll> > V;
cin>>N;
FOR(i,N) {
cin>>x>>y;
V.emplace_back(x,y);
nz+=x>0;
}
if(nz==0) {
FOR(y,N) FOR(x,y) V[y].second/=__gcd(V[x].second,V[y].second);
ll ret=1;
FOR(x,N) ret=ret*V[x].second%1000000007;
cout<<ret<<endl;
}
else {
cout << CRT_garner(V) << 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;
}
kmjp