結果

問題 No.458 異なる素数の和
ユーザー mbanmban
提出日時 2017-01-09 17:22:19
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 144 ms / 2,000 ms
コード長 2,442 bytes
コンパイル時間 3,533 ms
コンパイル使用メモリ 109,484 KB
実行使用メモリ 24,000 KB
最終ジャッジ日時 2023-09-18 15:16:17
合計ジャッジ時間 8,100 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 142 ms
22,060 KB
testcase_01 AC 142 ms
19,936 KB
testcase_02 AC 141 ms
21,928 KB
testcase_03 AC 141 ms
21,880 KB
testcase_04 AC 142 ms
23,920 KB
testcase_05 AC 143 ms
21,856 KB
testcase_06 AC 142 ms
21,856 KB
testcase_07 AC 143 ms
23,908 KB
testcase_08 AC 142 ms
21,980 KB
testcase_09 AC 141 ms
21,956 KB
testcase_10 AC 143 ms
21,788 KB
testcase_11 AC 142 ms
21,892 KB
testcase_12 AC 143 ms
23,948 KB
testcase_13 AC 142 ms
23,856 KB
testcase_14 AC 142 ms
23,840 KB
testcase_15 AC 143 ms
23,952 KB
testcase_16 AC 141 ms
21,796 KB
testcase_17 AC 141 ms
19,868 KB
testcase_18 AC 144 ms
21,960 KB
testcase_19 AC 141 ms
24,000 KB
testcase_20 AC 141 ms
23,996 KB
testcase_21 AC 144 ms
21,888 KB
testcase_22 AC 141 ms
21,952 KB
testcase_23 AC 143 ms
23,840 KB
testcase_24 AC 141 ms
22,004 KB
testcase_25 AC 142 ms
21,872 KB
testcase_26 AC 141 ms
21,912 KB
testcase_27 AC 142 ms
23,996 KB
testcase_28 AC 142 ms
23,988 KB
testcase_29 AC 143 ms
23,928 KB
testcase_30 AC 142 ms
21,900 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;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;


class Magatro
{
    static int N;
    static int[] Prime;
    static bool[] isPrime;
    const int Max = 20000;
    static void Main()
    {
        Scanner sc = new Scanner();
        N = sc.NextInt();
        PrimeCalc();
        int[] dp = (new int[Max + 1]).Select(s => -1).ToArray();
        dp[0] = 0;
        foreach(int i in Prime)
        {
            for(int j = Max-i; j >=0; j--)
            {
                if (dp[j] != -1)
                {
                    if (dp[j + i] < dp[j]+1)
                    {
                        dp[j + i] = dp[j] + 1;
                    }
                }
            }
        }
        Console.WriteLine(dp[N]);
    } 
    static void PrimeCalc()
    {
        int loop = (int)Math.Sqrt(Max);
        bool[] num = new bool[Max + 1];
        num[0] = num[1] = true;
        for(int i = 4; i <= Max; i += 2)
        {
            num[i] = true;
        }
        for(int i = 3; i <= loop; i += 2)
        {
            if (!num[i])
            {
                for (int j = i * 2; j <= Max; j += i)
                {
                    num[j] = true;
                }
            }
        }
        List<int> res = new List<int>();
        res.Add(2);
        for(int i = 3; i <= Max; i += 2)
        {
            if (!num[i])
            {
                res.Add(i);
            }
        }
        Prime = res.ToArray();
        isPrime = num;
    }
}
public class Scanner
{
    private string[] S;
    private int Index;
    private char Separator;
    public Scanner(char separator=' ')
    {
        Index = 0;
        Separator = separator;
    }
    public string NextString()
    {
        string result;
        if (S == null)
        {
            NextLine();
        }
        else if (Index >= S.Length)
        {
            NextLine();
        }
        result = S[Index];
        Index++;
        return result;
    }
    private void NextLine()
    {
        S = Console.ReadLine().Split(Separator);
        Index = 0;
    }    
    public int NextInt()
    {
        return int.Parse(NextString());
    }
    public double NextDouble()
    {
        return double.Parse(NextString());
    }
    public long NextLong()
    {
        return long.Parse(NextString());
    }
}
0