結果
| 問題 |
No.458 異なる素数の和
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2018-12-28 23:55:33 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 719 ms / 2,000 ms |
| コード長 | 3,554 bytes |
| コンパイル時間 | 847 ms |
| コンパイル使用メモリ | 118,696 KB |
| 実行使用メモリ | 198,528 KB |
| 最終ジャッジ日時 | 2024-10-01 15:03:30 |
| 合計ジャッジ時間 | 23,796 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| 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.
ソースコード
#pragma warning disable
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Numerics;
using System.Collections;
using System.Timers;
static class MainClass
{
public static void Main(string[] args)
{
var N = Console.ReadLine().ToInt32();
var ls = GeneratePrimes().Take(2300).ToList();
var count = ls.Count();
var dp = new int[count + 10,20010];
for (int i = 0; i < count; i++)
{
for (int j = 0; j < 20010; j++)
{
dp[i, j] = -INF;
}
}
dp[0, 0] = 0;
for (int i = 0; i < count; i++)
{
for (int j = 0; j < 20010; j++)
{
var item = ls[i];
if (j - item < 0)
{
dp[i + 1, j] = dp[i, j];
}
else
{
dp[i + 1, j] = Math.Max(dp[i, j], dp[i, j - ls[i]] + 1);
}
}
}
var num = dp[count, N];
Console.WriteLine(num < 0 ? -1 : num);
}
public class BoundedBoolArray
{
private BitArray _array;
private int _lower;
public BoundedBoolArray(int lower, int upper)
{
_array = new BitArray(upper - lower + 1);
_lower = lower;
}
public bool this[int index]
{
get
{
return _array[index - _lower];
}
set
{
_array[index - _lower] = value;
}
}
}
public static IEnumerable<int> GeneratePrimes()
{
var primes = new List<int>() { 2, 3 };
foreach (var p in primes)
yield return p;
int ix = 0;
while (true)
{
int prime1st = primes[ix];
int prime2nd = primes[++ix];
var lower = prime1st * prime1st;
var upper = prime2nd * prime2nd - 1;
var sieve = new BoundedBoolArray(lower, upper);
foreach (var prime in primes.Take(ix))
{
var start = (int)Math.Ceiling((double)lower / prime) * prime;
for (int index = start; index <= upper; index += prime)
sieve[index] = true;
}
for (int i = lower; i <= upper; i++)
{
if (sieve[i] == false)
{
primes.Add(i);
yield return i;
}
}
}
}
#region ライブラリ
public static long ToInt64(this string str) => long.Parse(str);
public static int ToInt32(this string str) => int.Parse(str);
public static BigInteger ToBigInteger(this string str) => BigInteger.Parse(str);
public static List<string> SplittedStringToList(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).ToList();
public static List<int> SplittedStringToInt32List(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)).ToList();
public static List<BigInteger> SplittedStringToBigInteger(this string str) => str.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries).Select(x => BigInteger.Parse(x)).ToList();
public const int INF = int.MaxValue / 2;
#endregion
}