結果

問題 No.407 鴨等素数間隔列の数え上げ
ユーザー はむ吉🐹はむ吉🐹
提出日時 2016-07-10 18:22:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 57 ms / 1,000 ms
コード長 1,354 bytes
コンパイル時間 1,031 ms
コンパイル使用メモリ 61,660 KB
実行使用メモリ 5,960 KB
最終ジャッジ日時 2024-05-09 04:23:15
合計ジャッジ時間 2,164 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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 = 20000000;


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


long long count_seqs(int n, int l) {
    auto x_max = [&](int d) {return l - (n - 1) * d; };
    auto d_max = l / (n - 1);
    if (d_max < 2)
    {
        return 0;
    }
    auto ds = sieve_of_eratosthenes(d_max + 1);
    long long ans = 0;
    for (const auto& d : ds)
    {
        ans += x_max(d) + 1;
    }
    return ans;
}


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