結果
| 問題 |
No.1661 Sum is Prime (Hard Version)
|
| コンテスト | |
| ユーザー |
SSRS
|
| 提出日時 | 2021-08-27 21:37:27 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 233 ms / 3,000 ms |
| コード長 | 2,694 bytes |
| コンパイル時間 | 1,799 ms |
| コンパイル使用メモリ | 168,972 KB |
| 実行使用メモリ | 85,632 KB |
| 最終ジャッジ日時 | 2024-12-30 16:48:13 |
| 合計ジャッジ時間 | 7,892 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 22 |
ソースコード
//https://old.yosupo.jp/submission/11199
#include<bits/stdc++.h>
using namespace std;
const int N = 3e5 + 9;
namespace pcf {
// initialize once by calling init()
#define MAXN 10000010 // initial sieve limit
#define MAX_PRIMES 1000010 // max size of the prime array for sieve
#define PHI_N 100000
#define PHI_K 100
unsigned int ar[(MAXN >> 6) + 5] = {0};
int len = 0; // total number of primes generated by sieve
int primes[MAX_PRIMES];
int counter[MAXN]; // counter[m] --> number of primes <= i
int dp[PHI_N][PHI_K]; // precal of yo(n,k)
bitset <MAXN> fl;
void sieve(int n) {
fl[1] = true;
for (int i = 4; i <= n; i += 2) fl[i] = true;
for (int i = 3; i * i <= n; i += 2) {
if (!fl[i]) {
for (int j = i * i; j <= n; j += i << 1) fl[j] = 1;
}
}
for (int i = 1; i <= n; i++) {
if (!fl[i]) primes[len++] = i;
counter[i] = len;
}
}
void init() {
sieve(MAXN - 1);
// precalculation of phi upto size (PHI_N,PHI_K)
int k, n, res;
for (n = 0; n < PHI_N; n++) dp[n][0] = n;
for (k = 1; k < PHI_K; k++) {
for (n = 0; n < PHI_N; n++) {
dp[n][k] = dp[n][k - 1] - dp[n / primes[k - 1]][k - 1];
}
}
}
// returns number of integers less or equal n which are
// not divisible by any of the first k primes
// recurrence --> yo(n , k) = yo(n , k-1) - yo(n / p_k , k-1)
// for sum of primes yo(n,k)=yo(n,k-1)-p_k*yo(n/p_k,k-1)
long long yo(long long n, int k) {
if (n < PHI_N && k < PHI_K) return dp[n][k];
if (k == 1) return ((++n) >> 1);
if (primes[k - 1] >= n) return 1;
return yo(n, k - 1) - yo(n / primes[k - 1], k - 1);
}
long long Legendre(long long n) {
if (n < MAXN) return counter[n];
int lim = sqrt(n) + 1;
int k = upper_bound(primes, primes + len, lim) - primes;
return yo(n, k) + (k - 1);
}
//complexity: n^(2/3).(log n^(1/3))
long long Lehmer(long long n) {
if (n < MAXN) return counter[n];
long long w, res = 0;
int i, j, a, b, c, lim;
b = sqrt(n), c = Lehmer(cbrt(n)), a = Lehmer(sqrt(b)), b = Lehmer(b);
res = yo(n, a) + (((b + a - 2) * (b - a + 1)) >> 1);
for (i = a; i < b; i++) {
w = n / primes[i];
lim = Lehmer(sqrt(w)), res -= Lehmer(w);
if (i <= c) {
for (j = i; j < lim; j++) {
res += j;
res -= Lehmer(w / primes[j]);
}
}
}
return res;
}
}
int32_t main() {
pcf::init();
long long L, R;
cin >> L >> R;
long long A = pcf::Lehmer(R) - pcf::Lehmer(L - 1);
long long B = pcf::Lehmer(R * 2) - pcf::Lehmer(L * 2);
cout << A + B << endl;
}
SSRS