結果
| 問題 |
No.1661 Sum is Prime (Hard Version)
|
| コンテスト | |
| ユーザー |
milanis48663220
|
| 提出日時 | 2021-08-27 22:36:42 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 40 ms / 3,000 ms |
| コード長 | 2,883 bytes |
| コンパイル時間 | 1,170 ms |
| コンパイル使用メモリ | 123,380 KB |
| 最終ジャッジ日時 | 2025-01-24 03:23:21 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 |
ソースコード
#include <algorithm>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <tuple>
#include <vector>
#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;
template <class T>
inline bool chmax(T& a, T b) {
if (a < b) {
a = b;
return 1;
}
return 0;
}
template <class T>
inline bool chmin(T& a, T b) {
if (a > b) {
a = b;
return 1;
}
return 0;
}
using namespace std;
typedef long long ll;
int isqrt(ll n) {
return sqrtl(n);
}
ll prime_pi(const ll N) {
if (N <= 1) return 0;
if (N == 2) return 1;
const int v = isqrt(N);
int s = (v + 1) / 2;
vector<int> smalls(s);
for (int i = 1; i < s; ++i) smalls[i] = i;
vector<int> roughs(s);
for (int i = 0; i < s; ++i) roughs[i] = 2 * i + 1;
vector<ll> larges(s);
for (int i = 0; i < s; ++i) larges[i] = (N / (2 * i + 1) - 1) / 2;
vector<bool> skip(v + 1);
const auto divide = [](ll n, ll d) -> int { return double(n) / d; };
const auto half = [](int n) -> int { return (n - 1) >> 1; };
int pc = 0;
for (int p = 3; p <= v; p += 2)
if (!skip[p]) {
int q = p * p;
if (ll(q) * q > N) break;
skip[p] = true;
for (int i = q; i <= v; i += 2 * p) skip[i] = true;
int ns = 0;
for (int k = 0; k < s; ++k) {
int i = roughs[k];
if (skip[i]) continue;
ll d = ll(i) * p;
larges[ns] = larges[k] - (d <= v ? larges[smalls[d >> 1] - pc] : smalls[half(divide(N, d))]) + pc;
roughs[ns++] = i;
}
s = ns;
for (int i = half(v), j = ((v / p) - 1) | 1; j >= p; j -= 2) {
int c = smalls[j >> 1] - pc;
for (int e = (j * p) >> 1; i >= e; --i) smalls[i] -= c;
}
++pc;
}
larges[0] += ll(s + 2 * (pc - 1)) * (s - 1) / 2;
for (int k = 1; k < s; ++k) larges[0] -= larges[k];
for (int l = 1; l < s; ++l) {
int q = roughs[l];
ll M = N / q;
int e = smalls[half(M / q)] - pc;
if (e < l + 1) break;
ll t = 0;
for (int k = l + 1; k <= e; ++k) t += smalls[half(divide(M, roughs[k]))];
larges[0] += t - ll(e - l) * (pc + l - 1);
}
return larges[0] + 1;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(10) << fixed;
ll l, r; cin >> l >> r;
if(l == 1 && r == 1){
cout << 0 << endl;
return 0;
}
ll ans = prime_pi(r)-prime_pi(l-1);
ans += prime_pi(r*2-1)-prime_pi(l*2);
cout << ans << endl;
}
milanis48663220