結果
問題 |
No.458 異なる素数の和
|
ユーザー |
![]() |
提出日時 | 2017-02-12 06:54:35 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 817 ms / 2,000 ms |
コード長 | 2,723 bytes |
コンパイル時間 | 895 ms |
コンパイル使用メモリ | 112,696 KB |
実行使用メモリ | 213,888 KB |
最終ジャッジ日時 | 2024-07-05 05:58:53 |
合計ジャッジ時間 | 6,938 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 28 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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(); } }