結果

問題 No.458 異なる素数の和
ユーザー GrenacheGrenache
提出日時 2016-12-09 00:27:21
言語 Java21
(openjdk 21)
結果
AC  
実行時間 203 ms / 2,000 ms
コード長 2,204 bytes
コンパイル時間 3,923 ms
コンパイル使用メモリ 76,120 KB
実行使用メモリ 56,204 KB
最終ジャッジ日時 2023-09-18 14:53:23
合計ジャッジ時間 9,837 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 131 ms
55,376 KB
testcase_01 AC 159 ms
55,760 KB
testcase_02 AC 164 ms
55,836 KB
testcase_03 AC 143 ms
55,744 KB
testcase_04 AC 143 ms
55,648 KB
testcase_05 AC 194 ms
56,204 KB
testcase_06 AC 163 ms
55,420 KB
testcase_07 AC 132 ms
55,872 KB
testcase_08 AC 193 ms
55,628 KB
testcase_09 AC 139 ms
55,716 KB
testcase_10 AC 130 ms
55,980 KB
testcase_11 AC 203 ms
55,548 KB
testcase_12 AC 129 ms
55,660 KB
testcase_13 AC 128 ms
55,908 KB
testcase_14 AC 126 ms
56,108 KB
testcase_15 AC 128 ms
55,836 KB
testcase_16 AC 139 ms
55,708 KB
testcase_17 AC 128 ms
55,772 KB
testcase_18 AC 129 ms
55,692 KB
testcase_19 AC 128 ms
55,684 KB
testcase_20 AC 128 ms
55,844 KB
testcase_21 AC 128 ms
55,916 KB
testcase_22 AC 128 ms
55,804 KB
testcase_23 AC 130 ms
55,680 KB
testcase_24 AC 132 ms
55,788 KB
testcase_25 AC 130 ms
55,528 KB
testcase_26 AC 129 ms
55,576 KB
testcase_27 AC 160 ms
55,948 KB
testcase_28 AC 201 ms
55,588 KB
testcase_29 AC 141 ms
55,748 KB
testcase_30 AC 152 ms
55,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


public class Main_yukicoder458 {

	private static Scanner sc;
	private static Printer pr;

	private static void solve() {
		int n = sc.nextInt();

		Prime prime = new Prime(n);
		List<Integer> primes = prime.getPrimeList();

		int[] dp = new int[n + 1];
		Arrays.fill(dp, -1);
		dp[0] = 0;
		for (int p : primes) {
			for (int i = n - p; i >= 0; i--) {
				if (dp[i] >= 0) {
					dp[i + p] = Math.max(dp[i + p], dp[i] + 1);
				}
			}
		}

		pr.println(dp[n]);
	}

	@SuppressWarnings("unused")
	private static class Prime {
		private int n;
		private List<Integer> primes;
		private BitSet p;

		Prime(int n) {
			this.n = n;

			p = new BitSet(n / 2);
			int m = (int)Math.sqrt(n);

//			for (int i = 1; i <= m; i++) {
			for (int bi = p.nextClearBit(0); 2 * (bi + 1) + 1 <= m; bi = p.nextClearBit(bi + 1)) {
				long i = bi + 1;
//				if (p.get(i - 1)) {
//					continue;
//				}

				for (long j = 2 * i * (i + 1); 2 * j + 1 <= n; j += 2 * i + 1) {
					p.set((int)(j - 1));
				}
			}
		}

		boolean isPrime(int n) {
			if (n == 2) {
				return true;
			}

			if (n == 1 || (n & 0x1) == 0) {
				return false;
			}

			return !p.get(n / 2 - 1);
		}

		List<Integer> getPrimeList() {
			if (primes != null) {
				return primes;
			}

			primes = new ArrayList<>();
			if (n >= 2) {
				primes.add(2);
//				for (int i = 1; 2 * i + 1 <= n; i++) {
				for (int bi = p.nextClearBit(0); 2 * (bi + 1) + 1 <= n; bi = p.nextClearBit(bi + 1)) {
					int i = bi + 1;
//					if (!p.get(i - 1)) {
//						primes.add(2 * i + 1);
//					}
					primes.add(2 * i + 1);
				}
			}

			return primes;
		}

		private static boolean isPrime(long n) {
			if (n == 2) {
				return true;
			}

			if (n == 1 || (n & 0x1) == 0) {
				return false;
			}

			for (long i = 3; i * i <= n; i += 2) {
				if (n % i == 0) {
					return false;
				}
			}

			return true;
		}
	}

	// ---------------------------------------------------
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		pr = new Printer(System.out);

		solve();

		pr.close();
		sc.close();
	}

	private static class Printer extends PrintWriter {
		Printer(PrintStream out) {
			super(out);
		}
	}
}
0