結果

問題 No.186 中華風 (Easy)
ユーザー koyumeishikoyumeishi
提出日時 2015-04-20 00:36:10
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 1,949 bytes
コンパイル時間 1,106 ms
コンパイル使用メモリ 78,488 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 00:44:12
合計ジャッジ時間 1,985 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,380 KB
testcase_01 AC 9 ms
4,376 KB
testcase_02 AC 7 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 14 ms
4,376 KB
testcase_05 AC 16 ms
4,380 KB
testcase_06 AC 15 ms
4,376 KB
testcase_07 AC 12 ms
4,380 KB
testcase_08 AC 22 ms
4,376 KB
testcase_09 AC 22 ms
4,376 KB
testcase_10 AC 12 ms
4,376 KB
testcase_11 AC 21 ms
4,384 KB
testcase_12 AC 11 ms
4,376 KB
testcase_13 AC 10 ms
4,380 KB
testcase_14 AC 11 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 8 ms
4,376 KB
testcase_17 AC 6 ms
4,376 KB
testcase_18 AC 5 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 3 ms
4,376 KB
testcase_21 AC 6 ms
4,376 KB
testcase_22 AC 2 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);
}


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

	long long ans = -1;
	long long ub = lcm(y[0], y[1]);
	for(long long val = x[0]; val<ub; val += y[0]){
		if(val % y[1] == x[1]){
			long long ub_ = lcm(ub, y[2]);
			for(long long val_ = val; val_<ub_; val_ += ub){
				if(val_ % y[2] == x[2]){
					if(ans < 0) ans = val_;
					else ans = min(ans, val_);
				}
			}
		}
	}

	if(ans == 0) ans = lcm(y[0], lcm(y[1],y[2]));
	cout << ans << endl;

	return 0;
}
0