結果
問題 |
No.458 異なる素数の和
|
ユーザー |
![]() |
提出日時 | 2017-01-09 17:22:19 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 95 ms / 2,000 ms |
コード長 | 2,442 bytes |
コンパイル時間 | 2,292 ms |
コンパイル使用メモリ | 108,160 KB |
実行使用メモリ | 19,456 KB |
最終ジャッジ日時 | 2024-07-05 05:56:12 |
合計ジャッジ時間 | 5,384 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
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.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()); } }