結果

問題 No.7 プライムナンバーゲーム
ユーザー YamaKasaYamaKasa
提出日時 2018-06-09 22:41:33
言語 Java21
(openjdk 21)
結果
AC  
実行時間 171 ms / 5,000 ms
コード長 1,155 bytes
コンパイル時間 2,376 ms
コンパイル使用メモリ 78,004 KB
実行使用メモリ 54,448 KB
最終ジャッジ日時 2024-04-09 04:29:31
合計ジャッジ時間 5,619 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 127 ms
54,224 KB
testcase_01 AC 128 ms
54,188 KB
testcase_02 AC 170 ms
54,324 KB
testcase_03 AC 152 ms
54,320 KB
testcase_04 AC 133 ms
54,000 KB
testcase_05 AC 121 ms
52,916 KB
testcase_06 AC 163 ms
54,252 KB
testcase_07 AC 152 ms
54,288 KB
testcase_08 AC 136 ms
53,424 KB
testcase_09 AC 161 ms
54,448 KB
testcase_10 AC 126 ms
54,344 KB
testcase_11 AC 151 ms
54,444 KB
testcase_12 AC 167 ms
54,284 KB
testcase_13 AC 159 ms
54,252 KB
testcase_14 AC 168 ms
54,424 KB
testcase_15 AC 171 ms
54,092 KB
testcase_16 AC 162 ms
54,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

public class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int N = scan.nextInt();
		scan.close();
		ArrayList<Integer> list = new ArrayList<Integer>();
		for(int i = 2; i <= N; i++) {
			if(isPrime(i)) {
				list.add(i);
			}
		}
		if(N == 2 || N == 3) {
			System.out.println("Lose");
			System.exit(0);
		}
		// a[i] = 1なら負け
		int []a = new int[N + 1];
		Arrays.fill(a, 1);

		for(int i = 2; i <= N; i++) {
			for(int j = 0; j < list.size(); j++) {
				int t = i - list.get(j);
				if(t <= 1) {
					a[i] = 1;
					break;
				}
				if(t <= 3 ) {
					a[i] = 0;
					break;
				}
				if(a[t] == 1) {
					a[i] = 0;
					break;
				}
			}
		}
		if(a[N] == 1) {
			System.out.println("Lose");
		}else {
			System.out.println("Win");
		}
	}


	public static boolean isPrime(int n) {
	    if (n < 2) return false;
	    else if (n == 2) return true;
	    else if (n % 2 == 0) return false;

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

	    return true;
	}
}
0