結果

問題 No.713 素数の和
ユーザー keymoonkeymoon
提出日時 2019-04-18 18:38:09
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 27 ms / 2,000 ms
コード長 1,896 bytes
コンパイル時間 1,112 ms
コンパイル使用メモリ 112,468 KB
実行使用メモリ 24,900 KB
最終ジャッジ日時 2023-10-23 16:18:55
合計ジャッジ時間 2,246 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
24,900 KB
testcase_01 AC 26 ms
24,900 KB
testcase_02 AC 25 ms
24,900 KB
testcase_03 AC 25 ms
24,900 KB
testcase_04 AC 24 ms
24,900 KB
testcase_05 AC 27 ms
24,900 KB
testcase_06 AC 26 ms
24,900 KB
testcase_07 AC 25 ms
24,900 KB
testcase_08 AC 25 ms
24,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.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);
            }
        }
    }
}
0