結果

問題 No.458 異なる素数の和
ユーザー shinwisteriashinwisteria
提出日時 2017-03-30 21:04:33
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 894 bytes
コンパイル時間 3,235 ms
コンパイル使用メモリ 75,544 KB
実行使用メモリ 56,800 KB
最終ジャッジ日時 2023-09-21 08:54:36
合計ジャッジ時間 10,064 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 120 ms
56,028 KB
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 184 ms
56,104 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 322 ms
56,308 KB
testcase_09 WA -
testcase_10 AC 116 ms
55,860 KB
testcase_11 AC 361 ms
56,264 KB
testcase_12 AC 128 ms
55,996 KB
testcase_13 AC 120 ms
55,832 KB
testcase_14 AC 123 ms
56,192 KB
testcase_15 AC 113 ms
55,892 KB
testcase_16 AC 167 ms
56,084 KB
testcase_17 AC 117 ms
55,976 KB
testcase_18 AC 114 ms
55,492 KB
testcase_19 AC 116 ms
56,120 KB
testcase_20 AC 118 ms
56,288 KB
testcase_21 AC 116 ms
55,752 KB
testcase_22 AC 112 ms
55,912 KB
testcase_23 WA -
testcase_24 AC 117 ms
55,992 KB
testcase_25 AC 114 ms
55,724 KB
testcase_26 AC 119 ms
56,160 KB
testcase_27 AC 225 ms
56,268 KB
testcase_28 AC 349 ms
55,776 KB
testcase_29 WA -
testcase_30 AC 199 ms
56,140 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class DifferentSosu {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int N = s.nextInt();
		s.close();
		ArrayList<Integer> p = new ArrayList<>();
		p.add(2);
		for(int i = 3;i <= N;i++){
			int a = 1;
			for(int j = 0;j < p.size();j++){
				if(i % p.get(j) == 0){
					a = 0;
					break;
				}
			}
			if(a == 1){
				p.add(i);
			}
		}
		//System.out.println(p);
		int[] root = new int[N+1];
		root[0] = 0;
		root[1] = -1;
		for(int i:p){
			for(int j = N;j >= 0;j--){
				if(j-i >= 0&&root[j-i] != -1){
					root[j] = Math.max(root[j-i] + 1, root[j]);
					if(root[j] == 1 && !p.contains(j)){
						root[j] = -1;
					}
				}
			}
			/*for(int k:root){
				System.out.print(k + "  ");
			}
			System.out.println();*/
		}

		System.out.println(root[N]);
		//System.out.println(t.get(N));

	}

}
0