結果

問題 No.458 異なる素数の和
ユーザー shinwisteria
提出日時 2017-03-30 19:11:28
言語 Java
(openjdk 23)
結果
WA  
実行時間 -
コード長 870 bytes
コンパイル時間 2,815 ms
コンパイル使用メモリ 78,716 KB
実行使用メモリ 56,248 KB
最終ジャッジ日時 2024-07-07 03:05:27
合計ジャッジ時間 7,567 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 22 WA * 6
権限があれば一括ダウンロードができます

ソースコード

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);
			}
		}
		int[][] root = new int[N+1][2];
		root[1][0] = -1;
		for(int i = 2;i <= N;i++){
			for(int q:p){
				if(i-q < 0){
					root[i][0] = -1;
					break;
				}else if(root[i-q][0] != -1 && root[i-q][1] < q){
					root[i][0] += root[i-q][0] + 1;
					root[i][1] = q;
					break;
				}
			}
			//System.out.println(root[i][0] + "  " + root[i][1]);
		}
		if(root[N][0] == 0){
			root[N][0] = -1;
		}
		System.out.println(root[N][0]);

	}

}
0