結果

問題 No.186 中華風 (Easy)
ユーザー sirogamichan1sirogamichan1
提出日時 2020-09-26 23:10:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 1,583 bytes
コンパイル時間 3,255 ms
コンパイル使用メモリ 167,908 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 01:04:24
合計ジャッジ時間 2,959 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 2 ms
4,384 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,384 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,384 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 1 ms
4,384 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,384 KB
testcase_13 AC 1 ms
4,384 KB
testcase_14 AC 2 ms
4,384 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 1 ms
4,380 KB
testcase_20 AC 1 ms
4,380 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:15: 警告: "DEBUG" が再定義されました
   15 | #define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;
      | 
main.cpp:14: 備考: ここが以前の宣言がある位置です
   14 | #define DEBUG(x) ;
      | 
main.cpp: 関数 ‘int main(int, char**)’ 内:
main.cpp:73:14: 警告: structured bindings only available with ‘-std=c++17’ or ‘-std=gnu++17’ [-Wc++17-extensions]
   73 |         auto [a, b] = ChineseRem(X, Y);
      |              ^

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using P = pair<ll, ll>;

const ll MOD = 1e9+7;
// const ll MOD = 998244353;
const ll INF = 1ll<<60;

#define FOR(i,a,b) for (ll i=(a);i<(ll)(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define DEBUG(x) ;
#define DEBUG(x) std::cerr << #x << " : " << (x) << std::endl;

int dx[4]{0, 1, 0, -1};
int dy[4]{1, 0, -1, 0};

////////////////////
// Chinese remainder theorem
inline ll mod(ll a, ll m)
{
	a %= m;
	if (a < 0) a += m;
	return a;
}

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

pair<ll, ll> ChineseRem(const vector<ll> &b, const vector<ll> &m)
{
	assert(b.size() == m.size());
	ll r = 0, M = 1;
	for (ll i = 0; i < (int)b.size(); ++i)
	{
		ll p, q;
		ll d = extGcd(M, m[i], p, q);
		if ((b[i] - r) % d != 0) return make_pair(0, -1);
		ll tmp = (b[i] - r) / d * p %  (m[i]/d);
		r += M * tmp;
		M *= m[i]/d;
	}
	return {mod(r, M), M};
}

// Chinese remainder theorem
// x = b_0(mod, m_0)
// x = b_1(mod, m_1)
// ...
// x = b_N-1(mod, m_N-1)
// <=>
// x = r(mod, m)
//
// call :   ChineseRem(vector<ll>b, vector<ll>)
// return : pair<ll, ll>(x, m)
// return(no solution) : pair<ll, ll>(0, -1)

int main(int argc, char **argv)
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	ll N; N = 3;
	vector<ll> X(N), Y(N);
	REP(i, N) cin >> X[i] >> Y[i];

	auto [a, b] = ChineseRem(X, Y);
	if (a == 0)
	{
		std::cout << b << std::endl;
	}
	else
	{
		std::cout << a << std::endl;
	}

	return 0;
}
0