結果

問題 No.176 2種類の切手
コンテスト
ユーザー bal4u
提出日時 2019-05-11 21:30:21
言語 C(gnu17)
(gcc 15.2.0)
コンパイル:
gcc-15 -O2 -std=gnu17 -Wno-error=implicit-function-declaration -Wno-error=implicit-int -Wno-error=incompatible-pointer-types -Wno-error=int-conversion -DONLINE_JUDGE -o a.out _filename_ -lm
実行:
./a.out
結果
AC  
実行時間 2 ms / 1,000 ms
コード長 745 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 162 ms
コンパイル使用メモリ 38,980 KB
最終ジャッジ日時 2026-02-22 03:21:46
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// yukicoder: No.176 2種類の切手
// 2019.5.11 bal4u

#include <stdio.h>

int gcd(int a, int b) {
	int r;
	while (b != 0) r = a % b, a = b, b = r;
	return a;
}

int main()
{
	int A, B, T, ans;

	scanf("%d%d%d", &A, &B, &T);
	if (A == 1 || T % A == 0 || T % B == 0) ans = T;
	else if (T <= A) ans = A;
	else if (T <= B) {
		ans = ((T-1)/A+1)*A;
		if (ans > B) ans = B;
	} else {
		int a, b, k, mi, g = gcd(A, B);
		long long t = (long long)A/g*B;
		if (T > t) ans = (((T-t)-1)/g+1)*g + t;
		else {
			ans = ((T-1)/A+1)*A;
			if ((k = ((T-1)/B+1)*B) < ans) ans = k;
			mi = A/g; if (T/B < mi) mi = T/B;
			for (b = 0; b <= mi; b++) {
				a = ((T-b*B)-1)/A+1;
				if ((k = a*A+b*B) < ans) ans = k;
			}
		}
	}
	printf("%d\n", ans);
	return 0;
}
0