結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-09 18:27:30
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,180 bytes
コンパイル時間 539 ms
コンパイル使用メモリ 60,768 KB
実行使用メモリ 13,184 KB
最終ジャッジ日時 2024-04-21 11:52:13
合計ジャッジ時間 2,253 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 AC 1 ms
5,248 KB
testcase_03 AC 6 ms
5,248 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 1 ms
5,376 KB
testcase_12 AC 1 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 1 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 1 ms
5,376 KB
testcase_17 AC 1 ms
5,376 KB
testcase_18 AC 1 ms
5,376 KB
testcase_19 AC 9 ms
5,376 KB
testcase_20 AC 24 ms
9,216 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 6 ms
5,376 KB
testcase_24 AC 7 ms
5,376 KB
testcase_25 AC 40 ms
13,184 KB
testcase_26 AC 13 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 5 ms
5,376 KB
testcase_29 AC 15 ms
5,376 KB
testcase_30 AC 3 ms
5,376 KB
testcase_31 AC 12 ms
5,376 KB
testcase_32 AC 28 ms
7,936 KB
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <ciso646>
#include <cstdlib>
#include <iostream>
#include <vector>


// テストケース検証用の定数
constexpr int MIN_N = 3;
constexpr int MAX_N = 1000000;
constexpr int MIN_L = 1;
constexpr int MAX_L = 5000000;


// Eratosthenesの篩により、素数表をつくる
std::vector<bool> sieve_of_eratosthenes(int end) {
    assert(end > 1);
    std::vector<bool> is_prime(end, true);
    is_prime[0] = false;
    is_prime[1] = false;
    for (int i = 2; i < end; i++)
    {
        if (is_prime[i]) {
            for (int j = 2 * i; j < end; j += i)
            {
                is_prime[j] = false;
            }
        }
    }
    return is_prime;
}


// 素数計数関数の表をつくる
// 上のEratosthenesの篩とは区間のとりかたが異なることに注意
std::vector<int> pcf_table(int last) {
    assert(last >= 1);
    auto is_prime = sieve_of_eratosthenes(last + 1);
    std::vector<int> pcf(last + 1);
    // 漸化式pcf[i] = pcf[i - 1] + f(i)で計算していく
    // ただしiが素数ならばf(i) = 1、さもなければf(i) = 0
    // 単純なボトムアップの動的計画法による
    for (int i = 1; i <= last; i++)
    {
        pcf[i] = pcf[i - 1] + int(is_prime[i]);
    }
    return pcf;
}


// 鴨等素数間隔列を数える
long long count_seqs(int n, int l) {
    // 初項x0のとりうる最大値
    auto x0_max = l - n + 1;
    if (x0_max < 0)
    {
        return 0;
    }
    // x0が決まれば、dの最大値d_maxが決まる
    auto d_max = [&](int x0) {return (l - x0) / (n - 1); };
    // max d_max(x0) = d_max(0)
    // そこまでの素数計数関数の表をつくる
    auto pcf = pcf_table(d_max(0));
    // 答えはintにおさまらないことがある
    long long answer = 0;
    for (auto x0 = 0; x0 <= x0_max; x0++)
    {
        answer += pcf[d_max(x0)];
    }
    return answer;
}


int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int n, l;
    std::cin >> n >> l;
    assert(MIN_N <= n and n <= MAX_N);
    assert(MIN_L <= l and l <= MAX_L);
    std::cout << count_seqs(n, l) << std::endl;
    return EXIT_SUCCESS;
}
0