結果

問題 No.7 プライムナンバーゲーム
ユーザー nanaenanae
提出日時 2017-03-02 21:20:49
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 6 ms / 5,000 ms
コード長 949 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 106,480 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-03 01:41:45
合計ジャッジ時間 1,674 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 6 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 3 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 3 ms
4,376 KB
testcase_12 AC 5 ms
4,376 KB
testcase_13 AC 6 ms
4,380 KB
testcase_14 AC 6 ms
4,380 KB
testcase_15 AC 6 ms
4,380 KB
testcase_16 AC 5 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.string, std.conv, std.array, std.algorithm;
import std.range, std.uni, std.math, std.container;
import core.bitop, std.datetime;

void main(){
    int N = readln.chomp.to!int;
    auto primes = get_primes(N);
    auto dp = new bool[](N + 1);
    dp[0] = dp[1] = true;

    foreach(i ; 2 .. N + 1){
        foreach(p ; primes){
            if(p > i) break;
            if(!dp[i - p]){
                dp[i] = true;
                break;
            }
        }
    }

    //stderr.writeln(dp);

    writeln(dp[N] ? "Win" : "Lose");
}

auto get_primes(int N){
    if(N < 2) throw new Exception("N must be over 1.");
    auto sieve = new bool[](N);
    sieve[] = true;
    sieve[0] = sieve[1] = false;

    foreach(p ; 2 .. N){
        if(p*p > N) break;
        if(!sieve[p]) continue;
        foreach(d ; iota(p*p, N, p)){
            sieve[d] = false;
        }
    }

    return iota(N).filter!(a => sieve[a]).array;
}
0