結果

問題 No.187 中華風 (Hard)
ユーザー koyumeishikoyumeishi
提出日時 2015-04-20 03:26:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 369 ms / 3,000 ms
コード長 2,303 bytes
コンパイル時間 639 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-17 23:22:25
合計ジャッジ時間 6,843 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
4,376 KB
testcase_01 AC 125 ms
4,376 KB
testcase_02 AC 273 ms
4,380 KB
testcase_03 AC 268 ms
4,376 KB
testcase_04 AC 369 ms
4,380 KB
testcase_05 AC 367 ms
4,380 KB
testcase_06 AC 368 ms
4,376 KB
testcase_07 AC 369 ms
4,384 KB
testcase_08 AC 221 ms
4,376 KB
testcase_09 AC 221 ms
4,380 KB
testcase_10 AC 221 ms
4,380 KB
testcase_11 AC 369 ms
4,380 KB
testcase_12 AC 368 ms
4,376 KB
testcase_13 AC 63 ms
4,376 KB
testcase_14 AC 63 ms
4,376 KB
testcase_15 AC 266 ms
4,376 KB
testcase_16 AC 271 ms
4,384 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 90 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 284 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 368 ms
4,376 KB
testcase_23 AC 1 ms
4,376 KB
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;
}

vector<pair<long long, long long> > prime_factorization(long long N){
	vector< pair<long long, long long> > ret;
	
	for(long long i=2; i*i<=N; i++){
		long long tmp = 0;
		while(N%i==0){
			tmp++;
			N/=i;
		}
		if(tmp>0){
			ret.push_back( make_pair(i, tmp) );
		}
	}
	if(N!=1){
		ret.push_back( make_pair(N, 1) );
	}
	return ret;
}

#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 all_zero = true;
	for(int i=0; i<N; i++){
		if(x[i] != 0) all_zero = false;
	}

	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;
	}


	//lcmを求めたいが、
	//途中の処理でgcd(y[i], y[j]) == 1になっているので単純に掛け合わせればおーけー
	if(all_zero){
		long long lcm_ = 1;
		for(int i=0; i<N; i++){
			lcm_ = (lcm_ * y[i]%MOD)%MOD;
		}
		ans = lcm_;
	}

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