結果

問題 No.1352 Three Coins
ユーザー tkmst201
提出日時 2021-01-26 17:04:41
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 71 ms / 2,000 ms
コード長 2,020 bytes
コンパイル時間 1,805 ms
コンパイル使用メモリ 196,268 KB
最終ジャッジ日時 2025-01-18 08:22:40
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) begin(v),end(v)
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using pii = pair<int, int>;
constexpr ll INF = 1ll<<30;
constexpr ll longINF = 1ll<<60;
constexpr ll MOD = 1000000007;
constexpr bool debug = false;
//---------------------------------//

/*
まず、gcd(A, B, C) != 1 の場合は gcd 倍の値しか表現できないため `INF`
xA + yB + zC で表現可能な数を調べる

y >= A の場合、x を B 増やし y を A 減らしても式の値は同じため、y は mod A での値 0 <= y < A のみ考えれば良い
z についても同様に 0 <= z < A とする。

yB + zC で表現可能な値以上で mod A で等しい値はすべて表現可能であることに注意すると、
yB + zC が取りうる値で mod A の値が 0 から A-1 まですべて取りうるなら AB + AC 以上の値は全て表現可能である。

拡張ユークリッドの互除法より、ある x', y' が存在して、x' B + y' C = gcd(B, C) =: g である
今、gcd(A, B, C) = gcd(g, C) = 1 であるので、f(x) = gx (mod. A) は全射から yB + zC が mod A で 0 から A-1 まですべて取りうることが言えた。
*/

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

int main() {
	int A, B, C;
	cin >> A >> B >> C;
	if (gcd(A, gcd(B, C)) > 1) { puts("INF"); return 0; }
	
	vector<bool> exist(A * (B + C), false);
	exist[0] = true;
	REP(i, exist.size()) {
		if (!exist[i]) continue;
		if (i + A < exist.size()) exist[i + A] = true;
		if (i + B < exist.size()) exist[i + B] = true;
		if (i + C < exist.size()) exist[i + C] = true;
	}
	
	int ans = 0;
	REP(i, exist.size()) ans += !exist[i];
	cout << ans << endl;
}
0