結果

問題 No.187 中華風 (Hard)
ユーザー chocoruskchocorusk
提出日時 2018-11-24 17:04:38
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,147 bytes
コンパイル時間 939 ms
コンパイル使用メモリ 103,388 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-26 04:57:25
合計ジャッジ時間 3,328 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 72 ms
4,380 KB
testcase_03 AC 71 ms
4,380 KB
testcase_04 AC 84 ms
4,376 KB
testcase_05 AC 85 ms
4,376 KB
testcase_06 AC 86 ms
4,380 KB
testcase_07 AC 84 ms
4,380 KB
testcase_08 AC 65 ms
4,380 KB
testcase_09 AC 64 ms
4,376 KB
testcase_10 AC 64 ms
4,380 KB
testcase_11 AC 84 ms
4,380 KB
testcase_12 AC 84 ms
4,380 KB
testcase_13 AC 45 ms
4,380 KB
testcase_14 AC 46 ms
4,380 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 71 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 85 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <unordered_map>
#include <unordered_set>
#include <random>
using namespace std;
typedef long long int ll;
typedef pair<ll, ll> P;
const ll MOD=1e9+7;
const ll MAX=4e4;
vector<ll> prime;
bool isprime[MAX];
void sieve(){
	for(ll i=3; i<MAX; i+=2){
		isprime[i]=1;
	}
	isprime[2]=1;
	prime.push_back(2);
	for(ll i=3; i<MAX; i++){
		if(isprime[i]){
			prime.push_back(i);
			for(ll j=2*i; j<MAX; j+=i){
				isprime[j]=0;
			}
		}
	}
	return;
}
ll extgcd(ll a, ll b, ll& x, ll& y){
	ll d=a;
	if(b!=0){
		d=extgcd(b, a%b, y, x);
		y-=(a/b)*x;
	}else{
		x=1, y=0;
	}
	return d;
}
ll inv(ll a, ll p){
	ll x, y;
	extgcd(a, p, x, y);
	if(x<0){
		return p-(-x%p);
	}else{
		return x%p;
	}
}
int main()
{
	int n; cin>>n;
	sieve();
	unordered_map<ll, P> mp;
	for(int i=0; i<n; i++){
		ll x, y;
		cin>>x>>y;
		ll y0=y;
		for(auto p:prime){
			if(y0%p==0){
				ll q=1;
				while(y0%p==0){
					y0/=p;
					q*=p;
				}
				auto itr=mp.find(p);
				if(itr==mp.end()){
					mp[p]=P(q, x%q);
				}else{
					ll x1=(itr->second).second;
					ll q1=(itr->second).first;
					ll q0=min(q1, q);
					if(x1%q0!=x%q0){
						cout<<-1<<endl;
						return 0;
					}
					if(q1<q) itr->second=P(q, x%q);
				}
			}
		}
		if(y0>1){
			ll p=y0, q=p;
			auto itr=mp.find(p);
			if(itr==mp.end()){
				mp[p]=P(q, x%q);
			}else{
				ll x1=(itr->second).second;
				ll q1=(itr->second).first;
				ll q0=min(q1, q);
				if(x1%q0!=x%q0){
					cout<<-1<<endl;
					return 0;
				}
				if(q1<q) itr->second=P(q, x%q);
			}
		}
	}
	ll t[10000];
	ll m[10000], b[10000];
	int c=0;
	for(auto p:mp){
		m[c]=p.second.first, b[c]=p.second.second;
		c++;
	}
	for(int i=0; i<c; i++){
		ll mp=1;
		ll r=b[i];
		for(int j=0; j<i; j++){
			r+=(m[i]-t[j]*mp%m[i]);
			r%=m[i];
			mp*=m[j];
			mp%=m[i];
		}
		t[i]=inv(mp, m[i])*r%m[i];
	}
	ll ans=0;
	ll mp0=1;
	for(int i=0; i<c; i++){
		ans+=(t[i]*mp0);
		ans%=MOD;
		mp0*=m[i];
		mp0%=MOD;
	}
	cout<<ans<<endl;
	return 0;
}
0