結果

問題 No.1352 Three Coins
ユーザー ks2mks2m
提出日時 2021-01-17 14:57:36
言語 Java21
(openjdk 21)
結果
AC  
実行時間 190 ms / 2,000 ms
コード長 1,646 bytes
コンパイル時間 2,758 ms
コンパイル使用メモリ 74,668 KB
実行使用メモリ 56,600 KB
最終ジャッジ日時 2023-08-24 23:06:35
合計ジャッジ時間 9,342 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 123 ms
55,908 KB
testcase_01 AC 125 ms
55,620 KB
testcase_02 AC 137 ms
55,720 KB
testcase_03 AC 141 ms
56,484 KB
testcase_04 AC 127 ms
55,852 KB
testcase_05 AC 141 ms
55,868 KB
testcase_06 AC 188 ms
56,292 KB
testcase_07 AC 122 ms
55,792 KB
testcase_08 AC 186 ms
55,920 KB
testcase_09 AC 141 ms
56,396 KB
testcase_10 AC 157 ms
56,196 KB
testcase_11 AC 135 ms
56,288 KB
testcase_12 AC 123 ms
55,376 KB
testcase_13 AC 135 ms
56,004 KB
testcase_14 AC 184 ms
56,384 KB
testcase_15 AC 136 ms
56,136 KB
testcase_16 AC 124 ms
55,596 KB
testcase_17 AC 126 ms
55,720 KB
testcase_18 AC 162 ms
56,556 KB
testcase_19 AC 122 ms
55,372 KB
testcase_20 AC 125 ms
55,708 KB
testcase_21 AC 187 ms
56,236 KB
testcase_22 AC 125 ms
55,732 KB
testcase_23 AC 126 ms
55,932 KB
testcase_24 AC 127 ms
55,644 KB
testcase_25 AC 126 ms
55,688 KB
testcase_26 AC 125 ms
55,828 KB
testcase_27 AC 126 ms
55,940 KB
testcase_28 AC 153 ms
55,644 KB
testcase_29 AC 123 ms
56,132 KB
testcase_30 AC 190 ms
56,600 KB
testcase_31 AC 130 ms
56,052 KB
testcase_32 AC 187 ms
56,560 KB
testcase_33 AC 120 ms
55,640 KB
testcase_34 AC 121 ms
55,676 KB
testcase_35 AC 122 ms
55,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) throws Exception {
		Scanner sc = new Scanner(System.in);
		int[] a = new int[3];
		for (int i = 0; i < 3; i++) {
			a[i] = sc.nextInt();
		}
		sc.close();

		int g = 0;
		for (int i = 0; i < 3; i++) {
			g = gcd(g, a[i]);
		}
		if (g > 1) {
			System.out.println("INF");
			return;
		}

		Arrays.sort(a);
		int[] c = new int[a[0]];
		Arrays.fill(c, Integer.MAX_VALUE);
		c[0] = 0;
		for (int i = 1; i < 3; i++) {
			for (int j = 1; j < c.length; j++) {
				int x = 0;
				int end = a[0] * a[i] / gcd(a[0], a[i]);
				while (x < end) {
					x += a[i];
					if (x % a[0] == j) {
						c[j] = Math.min(c[j], x / a[0]);
						break;
					}
				}
			}
		}

		int[] c2 = new int[a[1]];
		Arrays.fill(c2, Integer.MAX_VALUE);
		c2[0] = 0;
		for (int j = 1; j < c2.length; j++) {
			int x = 0;
			int end = a[1] * a[2] / gcd(a[1], a[2]);
			while (x < end) {
				x += a[2];
				if (x % a[1] == j) {
					c2[j] = Math.min(c2[j], x / a[1]);
					break;
				}
			}
		}

		for (int j = 1; j < c.length; j++) {
			int x = j;
			int e = 1;
			while (e < c[j]) {
				int j2 = x % a[1];
				int d = x / a[1];
				if (c2[j2] <= d) {
					c[j] = Math.min(c[j], x / a[0]);
					break;
				}
				x += a[0];
			}
		}

		int ans = 0;
		for (int i = 1; i < a[0]; i++) {
//			if (c[i] == Integer.MAX_VALUE) {
//				System.out.println("INF");
//				return;
//			}
			ans += c[i];
		}
		System.out.println(ans);
	}

	static int gcd(int a, int b) {
		return b == 0 ? a : gcd(b, a % b);
	}

//	static int lcm(int a, int b) {
//		return a * b / gcd(a, b);
//	}
}
0