結果
| 問題 |
No.713 素数の和
|
| コンテスト | |
| ユーザー |
keymoon
|
| 提出日時 | 2019-04-18 18:38:09 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 25 ms / 2,000 ms |
| コード長 | 1,896 bytes |
| コンパイル時間 | 1,123 ms |
| コンパイル使用メモリ | 116,252 KB |
| 実行使用メモリ | 24,876 KB |
| 最終ジャッジ日時 | 2024-09-22 10:40:14 |
| 合計ジャッジ時間 | 1,928 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 6 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Numerics;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using static System.Math;
using Debug = System.Diagnostics.Debug;
static class P
{
static void Main()
{
Console.WriteLine(Primes(int.Parse(Console.ReadLine())).Sum());
}
public static IEnumerable<int> Primes(int n)
{
if (n <= 32)
{
int[] primes = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
for (int i = 0; i < primes.Length; i++)
{
if (n < primes[i]) break;
else yield return primes[i];
}
yield break;
}
yield return 2;
int sup = (n + 1) / 32 / 2 + 1;
uint[] isp = new uint[sup];
int[] tprimes = { 3, 5, 7, 11, 13, 17, 19, 23, 29, 31 };
for (int i = 0; i < tprimes.Length; i++)
{
var tp = tprimes[i];
yield return tp;
uint[] ptn = new uint[tp];
for (int j = (tp - 3) / 2; j < (tp << 5); j += tp) ptn[j >> 5] |= 1U << (j & 31);
for (int j = 0; j < tp; j++) for (int l = j; l < sup; l += tp) isp[l] |= ptn[j];
}
int[] magic = { 0, 1, 23, 2, 29, 24, 19, 3, 30, 27, 25, 11, 20, 8, 4, 13, 31, 22, 28, 18, 26, 10, 7, 12, 21, 17, 9, 6, 16, 5, 15, 14 };
int h = n / 2;
for (int i = 0; i < sup; i++)
{
for (uint j = ~isp[i]; j != 0; j &= j - 1)
{
int pp = i << 5 | magic[((uint)(j & -j) * 0b0000111011010111110011000101001U) >> 27];
int p = 2 * pp + 3;
if (p > n) break;
yield return p;
for (int q = pp; q <= h; q += p) isp[q >> 5] |= 1U << (q & 31);
}
}
}
}
keymoon