結果

問題 No.7 プライムナンバーゲーム
ユーザー 14番14番
提出日時 2016-03-29 01:20:25
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 179 ms / 5,000 ms
コード長 3,074 bytes
コンパイル時間 2,132 ms
コンパイル使用メモリ 115,152 KB
実行使用メモリ 26,076 KB
最終ジャッジ日時 2024-04-09 03:57:18
合計ジャッジ時間 3,260 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 27 ms
23,824 KB
testcase_01 AC 26 ms
24,204 KB
testcase_02 AC 179 ms
23,820 KB
testcase_03 AC 36 ms
23,692 KB
testcase_04 AC 28 ms
24,128 KB
testcase_05 AC 28 ms
23,820 KB
testcase_06 AC 69 ms
25,948 KB
testcase_07 AC 55 ms
24,156 KB
testcase_08 AC 38 ms
21,928 KB
testcase_09 AC 93 ms
26,076 KB
testcase_10 AC 28 ms
23,816 KB
testcase_11 AC 55 ms
24,032 KB
testcase_12 AC 134 ms
23,776 KB
testcase_13 AC 141 ms
23,652 KB
testcase_14 AC 179 ms
24,296 KB
testcase_15 AC 170 ms
23,816 KB
testcase_16 AC 158 ms
24,160 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections.Generic;
using System.Text;

public class Program
{
    public void Proc(){
        Reader.IsDebug = false;
        int max = int.Parse(Reader.ReadLine());
        this.SetPrimeList(max);
        this.SetCanWinDic(max);
        bool ans = false;
        if(CanWin.ContainsKey(max)) {
            ans = this.CanWin[max];
        }
        Console.WriteLine(ans?"Win":"Lose");
    }
    
    private void SetCanWinDic(int max) {
        this.CanWin.Add(2, false);
        if(max >= 3) {
            this.CanWin.Add(3, false);
        }
        List<int> keyList = new List<int>();
        keyList.AddRange(CanWin.Keys);
        foreach (int key in keyList)
        {
            foreach (int prime in this.PrimeList)
            {
                int newKey = key + prime;
                if(newKey <= max) {
                    if(!CanWin.ContainsKey(newKey)) {
                        CanWin.Add(newKey, true);
                    }
                } else
                {
                    break;
                }
            }
        }
        
        for(int i=4; i<=max; i++) {
            if(!CanWin.ContainsKey(i)) {
                // Console.WriteLine(i + ":" + this.PrimeList[this.NearPrimeIndex[i]]);
                bool hasWin = false;
                for(int j=0; j<=this.NearPrimeIndex[i]; j++) {
                    int prime = this.PrimeList[j];
                    if(i - prime >= 2 && (!CanWin[i-prime])) {
                        hasWin = true;
                    }
                }
                CanWin.Add(i, hasWin);
            }
        }
    }
    
    private Dictionary<int, bool> CanWin = new Dictionary<int, bool>();
    
    private List<int> PrimeList = new List<int>();
    
    private int[] NearPrimeIndex;
    
    private void SetPrimeList(int max) {
        bool[] tempList = new bool[max+1];
        this.NearPrimeIndex = new int[max + 1];
        for(int i=2; i<=max; i++) {
            if(!tempList[i]) {
                PrimeList.Add(i);
                for(int j=1; j<=max / i; j++) {
                    tempList[j*i] = true;
                }
            }
            this.NearPrimeIndex[i] = this.PrimeList.Count - 1;
        }
    }
    
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}

class Reader
{
    public static bool IsDebug = true;
    
    private static System.IO.StringReader sr;
    
    public static string ReadLine() {
        if(IsDebug) {
            if(sr == null) {
                sr = new System.IO.StringReader(initStr.Trim());
            }
            return sr.ReadLine();
        } else {
            return Console.ReadLine();
        }
    }
    
    public static int[] GetInt(char delimiter = ' ') {
        string[] inpt = ReadLine().Split(delimiter);
        int[] ret = new int[inpt.Length];
        for(int i=0; i<inpt.Length; i++) {
            ret[i] = int.Parse(inpt[i]);
        }
        return ret;
    }
    
    private static string initStr = @"



10000



    ";
}

0