結果

問題 No.385 カップ麺生活
ユーザー mbanmban
提出日時 2017-04-13 00:37:29
言語 Java21
(openjdk 21)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 1,409 bytes
コンパイル時間 3,507 ms
コンパイル使用メモリ 77,972 KB
実行使用メモリ 54,528 KB
最終ジャッジ日時 2024-04-15 07:38:25
合計ジャッジ時間 9,362 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
53,880 KB
testcase_01 AC 134 ms
54,528 KB
testcase_02 AC 133 ms
54,292 KB
testcase_03 AC 133 ms
53,920 KB
testcase_04 AC 134 ms
54,336 KB
testcase_05 AC 133 ms
54,292 KB
testcase_06 AC 138 ms
54,372 KB
testcase_07 AC 134 ms
54,020 KB
testcase_08 AC 136 ms
54,292 KB
testcase_09 AC 133 ms
54,104 KB
testcase_10 AC 151 ms
54,452 KB
testcase_11 AC 133 ms
54,004 KB
testcase_12 AC 140 ms
53,924 KB
testcase_13 AC 140 ms
54,328 KB
testcase_14 AC 139 ms
53,956 KB
testcase_15 AC 138 ms
54,112 KB
testcase_16 AC 133 ms
53,976 KB
testcase_17 AC 146 ms
54,292 KB
testcase_18 AC 139 ms
54,076 KB
testcase_19 AC 136 ms
54,432 KB
testcase_20 AC 128 ms
53,076 KB
testcase_21 AC 139 ms
54,180 KB
testcase_22 AC 140 ms
53,984 KB
testcase_23 AC 138 ms
54,040 KB
testcase_24 AC 137 ms
54,360 KB
testcase_25 AC 134 ms
54,236 KB
testcase_26 AC 136 ms
54,508 KB
testcase_27 AC 136 ms
54,216 KB
testcase_28 AC 137 ms
54,188 KB
testcase_29 AC 134 ms
54,008 KB
testcase_30 AC 139 ms
54,164 KB
testcase_31 AC 134 ms
53,996 KB
testcase_32 AC 132 ms
54,412 KB
testcase_33 AC 131 ms
53,144 KB
testcase_34 AC 134 ms
54,220 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Program {
	public static void main(String[] args) {
		new Magatro().solve();
	}
}

class Magatro {
	private int m, n;
	private int[] c;
	private int[] dp;
	private List<Integer> primes;

	private void scan() {
		Scanner scanner = new Scanner(System.in);

		m = scanner.nextInt();
		n = scanner.nextInt();
		c = new int[n];

		for (int i = 0; i < n; i++) {
			c[i] = scanner.nextInt();
		}

		// scanner.close();
	}

	private void primeCalc() {
		primes = new ArrayList<Integer>();
		boolean[] bs = new boolean[m + 1];
		bs[0] = true;
		bs[1] = true;
		for (int i = 2; i * i <= m; i++) {
			if (!bs[i]) {
				for (int j = i * 2; j <= m; j += i) {
					bs[j] = true;
				}
			}
		}
		for (int i = 2; i <= m; i++) {
			if (!bs[i]) {
				primes.add(i);
			}
		}

	}

	public void solve() {
		scan();

		dp = new int[m + 1];

		for (int i = 0; i <= m; i++) {
			dp[i] = -1;
		}

		dp[m] = 0;

		for (int i = m; i >= 0; i--) {
			if (dp[i] == -1) {
				continue;
			}

			for (int j : c) {
				if (i - j < 0) {
					continue;
				}

				dp[i - j] = Math.max(dp[i - j], dp[i] + 1);
			}
		}

		primeCalc();
		int ans = 0;
		for (int i : primes) {
			if (dp[i] == -1) {
				continue;
			}

			ans += dp[i];
		}

		int max = 0;
		for (int i : dp) {
			max = Math.max(max, i);
		}
		ans += max;

		System.out.println(ans);
	}
}
0