結果

問題 No.186 中華風 (Easy)
ユーザー hornistyfhornistyf
提出日時 2021-03-15 23:07:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,126 bytes
コンパイル時間 1,309 ms
コンパイル使用メモリ 161,140 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 02:27:56
合計ジャッジ時間 2,240 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 1 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//yuki186.cpp
//Mon Mar 15 22:59:39 2021

#include <bits/stdc++.h>
#define INTINF 2147483647
#define LLINF 9223372036854775807
#define MOD 1000000007
#define rep(i,n) for (int i=0;i<(n);++i)

using namespace std;
using ll=long long;
typedef pair<int,int> P;

inline ll mod(ll a, ll m){
	return (a%m+m)%m;
}

ll extGCD(ll a, ll b, ll &p, ll &q){
	if (b == 0){
		p = 1;
		q = 0;
		return a;
	}
	ll d = extGCD(b, a%b, q, p);
	q -= a/b*p;
	return d;
}

//中国剰余定理。
//リターンは(r,m)。x ≡ r (mod m)
//解なしの時は(0,-1)が帰ってくる。
pair<ll,ll> ChineseRem(ll b1, ll m1, ll b2, ll m2){
	ll p,q;
	ll d = extGCD(m1,m2,p,q);
	if ((b2-b1)%d!=0) return make_pair(0,-1);
	ll m = m1*(m2/d); 
	ll tmp = (b2-b1)/d*p%(m2/d);
	ll r = mod(b1+m1*tmp,m);
	return make_pair(r,m);
}

int main(){
	vector<ll> bs(3),ms(3);
	rep(i,3) cin >> bs[i] >> ms[i];
	ll ansb = 0, ansm = 1;
	rep(i,3){
		pair<ll,ll> tmp = ChineseRem(ansb,ansm,bs[i],ms[i]);
		ansb = tmp.first;
		ansm = tmp.second;
		if (ansm==-1)break;
	}
	if (ansm==-1)cout << -1 << endl;
	else if (ansb==0) cout << ansm << endl;
	else cout << ansb << endl;
}
0