結果

問題 No.458 異なる素数の和
ユーザー 14番14番
提出日時 2017-02-12 06:54:35
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,062 ms / 2,000 ms
コード長 2,723 bytes
コンパイル時間 3,249 ms
コンパイル使用メモリ 103,888 KB
実行使用メモリ 217,108 KB
最終ジャッジ日時 2023-09-18 15:20:41
合計ジャッジ時間 11,746 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
20,748 KB
testcase_01 AC 317 ms
78,732 KB
testcase_02 AC 395 ms
97,988 KB
testcase_03 AC 107 ms
34,336 KB
testcase_04 AC 123 ms
38,088 KB
testcase_05 AC 891 ms
186,584 KB
testcase_06 AC 405 ms
93,804 KB
testcase_07 AC 58 ms
20,952 KB
testcase_08 AC 904 ms
185,880 KB
testcase_09 AC 73 ms
24,852 KB
testcase_10 AC 58 ms
20,684 KB
testcase_11 AC 1,062 ms
217,108 KB
testcase_12 AC 58 ms
20,696 KB
testcase_13 AC 58 ms
22,720 KB
testcase_14 AC 58 ms
20,648 KB
testcase_15 AC 57 ms
22,780 KB
testcase_16 AC 86 ms
28,400 KB
testcase_17 AC 58 ms
22,716 KB
testcase_18 AC 58 ms
20,732 KB
testcase_19 AC 58 ms
22,836 KB
testcase_20 AC 57 ms
20,752 KB
testcase_21 AC 57 ms
22,660 KB
testcase_22 AC 56 ms
20,648 KB
testcase_23 AC 58 ms
20,688 KB
testcase_24 AC 58 ms
22,784 KB
testcase_25 AC 57 ms
20,664 KB
testcase_26 AC 57 ms
20,736 KB
testcase_27 AC 368 ms
88,084 KB
testcase_28 AC 1,037 ms
213,172 KB
testcase_29 AC 64 ms
22,580 KB
testcase_30 AC 235 ms
62,908 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.Linq;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Text;
 
public class Program
{
    public void Proc() {
        Reader.IsDebug = false;
        this.Num = int.Parse(Reader.ReadLine());
        this.InitPrimeList(this.Num);
        this.memo = new Nullable<int>[this.PrimeList.Length, this.Num+1];
        int ans = this.GetAns(0, this.Num);
        Console.WriteLine(ans);
    }

    private Nullable<int>[,] memo;
    private Dictionary<int, Dictionary<int, int>> dic = new Dictionary<int, Dictionary<int, int>>();
    
    private int GetAns(int idx, int remain) {
        if(idx >= this.PrimeList.Length) {
            if(remain == 0) {
                return 0;
            } else {
                return -1;
            }
        }
        if(remain == 0) {
            return 0;
        }
        if(remain < 2) {
            return -1;
        }
        if(this.memo[idx, remain] != null) {
            return this.memo[idx, remain].Value;
        }
        int ans = -1;
        if(PrimeList[idx] <= remain) {
            int ret = this.GetAns(idx+1, remain-PrimeList[idx]);
            if(ret >= 0) {
                ret++;
                ans = Math.Max(ans, ret);
            }
            ret = this.GetAns(idx+1, remain);
            if(ret >= 0) {
                ans = Math.Max(ans, ret);
            }
        } else {
            int ret = this.GetAns(idx+1, remain);
            if(ret >= 0) {
                ans = Math.Max(ans, ret);
            }
        }
        this.memo[idx, remain] = ans;
        return ans;
    }
    private int[] PrimeList;

    private void InitPrimeList(int num) {
        bool[] flags = new bool[num+1];
        List<int> list = new List<int>();
        for(int i=2; i<=num; i++) {
            if(flags[i]) {
                continue;
            }
            list.Add(i);
            for(int j=1; j*i<=num; j++) {
                flags[i*j] = true;
            }
        }
        list.Reverse();
        this.PrimeList = list.ToArray();
    }

    private int Num;


    public class Reader {
        public static bool IsDebug = true;
        private static System.IO.StringReader SReader;
        private static string InitText = @"


20000



";
        public static string ReadLine() {
            if(IsDebug) {
                if(SReader == null) {
                    SReader = new System.IO.StringReader(InitText.Trim());
                }
                return SReader.ReadLine();
            } else {
                return Console.ReadLine();
            }
        }
    }
    public static void Main(string[] args)
    {
        Program prg = new Program();
        prg.Proc();
    }
}
0