結果

問題 No.187 中華風 (Hard)
ユーザー koyumeishikoyumeishi
提出日時 2015-04-20 03:16:11
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,509 bytes
コンパイル時間 913 ms
コンパイル使用メモリ 79,848 KB
実行使用メモリ 5,320 KB
最終ジャッジ日時 2023-09-17 23:22:06
合計ジャッジ時間 7,101 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
4,380 KB
testcase_01 AC 125 ms
4,376 KB
testcase_02 AC 276 ms
4,376 KB
testcase_03 AC 270 ms
4,384 KB
testcase_04 AC 370 ms
4,380 KB
testcase_05 AC 369 ms
4,376 KB
testcase_06 AC 369 ms
4,380 KB
testcase_07 AC 369 ms
4,376 KB
testcase_08 AC 222 ms
4,508 KB
testcase_09 AC 222 ms
4,380 KB
testcase_10 AC 222 ms
4,384 KB
testcase_11 AC 369 ms
4,380 KB
testcase_12 AC 369 ms
4,380 KB
testcase_13 AC 63 ms
4,384 KB
testcase_14 AC 64 ms
4,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 90 ms
4,380 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 282 ms
4,380 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 370 ms
4,380 KB
testcase_23 WA -
testcase_24 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cstdio>
#include <sstream>
#include <map>
#include <string>
#include <algorithm>
#include <queue>
#include <cmath>
#include <set>
using namespace std;

long long gcd(long long a, long long b){
	if(b==0) return a;
	return gcd(b, a%b);
}

long long lcm(long long a, long long b){
	if(a<b) swap(a,b);
	if(b==1) return a;
	return a * (b/gcd(a,b));
}


long long extgcd(long long a, long long b, long long &x, long long &y){
	long long d=a;
	if(b!=0){
		d = extgcd(b, a%b, y, x);
		y -= (a/b) * x;
	}else{
		x = 1;
		y = 0;
	}
	return d;
}

long long mod_inverse(long long a, long long m){
	long long x,y;
	extgcd(a,m,x,y);
	return (m+x%m)%m;
}


// return x where x mod p_i = value_i
// v[i] = pair{ value_i , p_i }
long long chinese_remainder_theorem(const vector<pair<long long,long long>>& v){
	long long M = 1;
	for(int i=0; i<v.size(); i++){
		M = lcm(M,v[i].second);
	}

	long long ret = 0;
	for(int i=0; i<v.size(); i++){
		long long M_i = (M/v[i].second);
		ret += (v[i].first * M_i * mod_inverse( M_i%v[i].second, v[i].second ))%M;
		ret = ret%M;
	}
	return ret%M;
}

// return x where x mod p_i = value_i
// v[i] = pair{ value_i , p_i }
long long chinese_remainder_theorem(const vector<long long>& value, const vector<long long>& mod){
	vector<pair<long long,long long>> v(value.size());
	for(int i=0; i<v.size(); i++){
		v[i] = {value[i], mod[i]};
	}
	return chinese_remainder_theorem(v);
}

#define MOD 1000000007

int main(){
	int N = 3;
	cin >> N;
	vector<long long> x(N), y(N);
	for(int i=0; i<N; i++){
		cin >> x[i] >> y[i];
	}

	bool valid = true;

	for(int i=0; i<N; i++){
		for(int j=i+1; j<N; j++){
			if(i == j) continue;
			long long g = gcd(y[i], y[j]);

			if( x[i]%g != x[j]%g ) valid = false;

			if(g != 1){
				y[i] /= g; y[j] /= g;
				long long g_ = gcd(y[i], g);
				while(g_ != 1){
					y[i] *= g_;
					g /= g_;
					g_ = gcd(y[i], g);
				}
				y[j] *= g;

				x[i] %= y[i];
				x[j] %= y[j];
			}
		}
	}

	if(!valid){
		cout << -1 << endl;
		return 0;
	}

	vector<long long> z(N);
	for(int i=0; i<N; i++){
		z[i] = x[i];
		for(int j=0; j<i; j++){
			z[i] = mod_inverse(y[j], y[i])%y[i] * (z[i] - z[j])%y[i];

			z[i] = (z[i]+y[i])%y[i];
		}
	}

	long long ans = 0;
	long long tmp = 1;
	for(int i=0; i<N; i++){
		ans = (ans + z[i] * tmp)%MOD;
		tmp = (tmp * y[i])%MOD;
	}

	if(ans == 0){
		long long lcm_ = 1;
		for(int i=0; i<N; i++){
			lcm_ = lcm(lcm_, y[i])%MOD;
		}
		ans = lcm_/*%MOD*/;
	}

	cout << ans << endl;
	return 0;
}
0