結果

問題 No.7 プライムナンバーゲーム
ユーザー magurogumamaguroguma
提出日時 2017-08-06 19:40:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,546 ms / 5,000 ms
コード長 1,487 bytes
コンパイル時間 265 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,008 KB
最終ジャッジ日時 2024-04-09 04:22:29
合計ジャッジ時間 11,620 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
11,008 KB
testcase_01 AC 30 ms
10,880 KB
testcase_02 AC 1,546 ms
10,880 KB
testcase_03 AC 122 ms
10,880 KB
testcase_04 AC 52 ms
10,880 KB
testcase_05 AC 53 ms
10,752 KB
testcase_06 AC 484 ms
10,880 KB
testcase_07 AC 338 ms
11,008 KB
testcase_08 AC 155 ms
10,880 KB
testcase_09 AC 694 ms
11,008 KB
testcase_10 AC 29 ms
11,008 KB
testcase_11 AC 323 ms
10,880 KB
testcase_12 AC 1,105 ms
10,880 KB
testcase_13 AC 1,203 ms
10,880 KB
testcase_14 AC 1,505 ms
11,008 KB
testcase_15 AC 1,461 ms
10,880 KB
testcase_16 AC 1,381 ms
11,008 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# -*- coding: utf-8 -*-

# 自分の番に2,3が来たら負け
# 自分の番に4,5,6,7,8,9,10が来れば勝ち
# 2,3,5,7,11,13,17,19,...
# 自分が引いたあとの数は2以上でなければ勝てないので,引く素数pはp<N'-1でないとダメ

"""
n以下の素数リストを返す(エラトステネスの篩)
"""
def make_prime_numbers(n):
    prime_list = []
    sieve = [False] * (n+1)
    for i in range(2, int(pow(n,0.5))+1, 1):
        if not sieve[i]:
            prime_list.append(i)    # 素数の追加
        # 素数の倍数のふるい落とし
        for j in range(i, n+1, i):
            sieve[j] = True
    # 残った素数の追加
    for i in range(int(pow(n,0.5)), n+1, 1):
        if not sieve[i]:
            prime_list.append(i)
    return prime_list

if __name__ == '__main__':
    n = int(input())
    dp = [False for i in range(10001)]   # 相手の手番の際の勝ち負けを記録
    dp[0] = dp[1] = True                 # 0,1は相手の手番だと負け
    p_list = make_prime_numbers(n)

    # n==2以降の全ての勝敗可能かのチェック
    for i in range(2, n+1, 1):
        # 全ての素数候補を考える
        for p in p_list:
            if i-p<0:
                break
            else:
                dp[i] = (dp[i] or (not dp[i-p])) # 素数pを引いた数の相手の勝ち負けの反転が,自身の勝ち負けとなる
    
    if dp[n]:
        print('Win')
    else:
        print('Lose')
0