結果
| 問題 |
No.407 鴨等素数間隔列の数え上げ
|
| コンテスト | |
| ユーザー |
はむ吉🐹
|
| 提出日時 | 2016-07-10 18:22:13 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 50 ms / 1,000 ms |
| コード長 | 1,354 bytes |
| コンパイル時間 | 709 ms |
| コンパイル使用メモリ | 63,032 KB |
| 実行使用メモリ | 6,820 KB |
| 最終ジャッジ日時 | 2024-12-15 13:51:50 |
| 合計ジャッジ時間 | 2,065 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 5 |
| other | AC * 31 |
ソースコード
#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;
}
はむ吉🐹