結果

問題 No.7 プライムナンバーゲーム
ユーザー YamaKasa
提出日時 2018-06-09 22:40:44
言語 Java
(openjdk 23)
結果
AC  
実行時間 146 ms / 5,000 ms
コード長 1,205 bytes
コンパイル時間 1,879 ms
コンパイル使用メモリ 78,656 KB
実行使用メモリ 41,768 KB
最終ジャッジ日時 2024-10-01 16:12:16
合計ジャッジ時間 4,689 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

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);
		a[0] = 1;
		a[1] = 1;
		a[2] = 1;
		a[3] = 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