結果

問題 No.1352 Three Coins
ユーザー ks2mks2m
提出日時 2021-01-17 14:35:29
言語 Java21
(openjdk 21)
結果
TLE  
実行時間 -
コード長 1,682 bytes
コンパイル時間 1,983 ms
コンパイル使用メモリ 78,280 KB
実行使用メモリ 62,684 KB
最終ジャッジ日時 2024-05-07 06:01:14
合計ジャッジ時間 6,542 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
46,872 KB
testcase_01 AC 128 ms
41,532 KB
testcase_02 AC 160 ms
41,804 KB
testcase_03 AC 167 ms
42,372 KB
testcase_04 AC 163 ms
41,704 KB
testcase_05 AC 209 ms
42,852 KB
testcase_06 TLE -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

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[3][a[2]];
		for (int i = 0; i < 3; i++) {
			Arrays.fill(c[i], Integer.MAX_VALUE);
			int i2 = (i + 1) % 3;
			for (int j = 1; j < a[i]; j++) {
				int x = 0;
				int end = a[i] * a[i2] / gcd(a[i], a[i2]);
				while (x < end) {
					x += a[i2];
					if (x % a[i] == j) {
						c[i][j] = Math.min(c[i][j], x / a[i]);
						break;
					}
				}
			}
		}

		int lcm = 1;
		for (int i = 0; i < 3; i++) {
			lcm = lcm(lcm, a[i]);
		}
		for (int i = 1; i < 3; i++) {
			for (int j = 1; j < a[0]; j++) {
				for (int j2 = 1; j2 < a[i]; j2++) {
					if (c[i][j2] == Integer.MAX_VALUE) {
						continue;
					}
					int x = a[i] * c[i][j2] + j2;
					int end = lcm * 2;
					if (c[0][j] != Integer.MAX_VALUE) {
						end = Math.min(end, a[0] * c[0][j] + j);
					}
					while (x < end) {
						if (x % a[0] == j) {
							c[0][j] = Math.min(c[0][j], x / a[0]);
							break;
						}
						x += a[i];
					}
				}
			}
		}

		int ans = 0;
		for (int i = 1; i < a[0]; i++) {
			if (c[0][i] == Integer.MAX_VALUE) {
				System.out.println("INF");
				return;
			}
			ans += c[0][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