結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー SSRSSSRS
提出日時 2021-08-27 21:37:27
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 164 ms / 3,000 ms
コード長 2,694 bytes
コンパイル時間 1,527 ms
コンパイル使用メモリ 167,148 KB
実行使用メモリ 85,716 KB
最終ジャッジ日時 2023-08-13 08:15:17
合計ジャッジ時間 5,988 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 124 ms
85,372 KB
testcase_01 AC 124 ms
85,508 KB
testcase_02 AC 143 ms
85,368 KB
testcase_03 AC 126 ms
85,308 KB
testcase_04 AC 126 ms
85,372 KB
testcase_05 AC 126 ms
85,320 KB
testcase_06 AC 126 ms
85,380 KB
testcase_07 AC 126 ms
85,444 KB
testcase_08 AC 124 ms
85,368 KB
testcase_09 AC 125 ms
85,360 KB
testcase_10 AC 128 ms
85,444 KB
testcase_11 AC 126 ms
85,320 KB
testcase_12 AC 145 ms
85,504 KB
testcase_13 AC 138 ms
85,412 KB
testcase_14 AC 145 ms
85,384 KB
testcase_15 AC 149 ms
85,444 KB
testcase_16 AC 143 ms
85,508 KB
testcase_17 AC 144 ms
85,440 KB
testcase_18 AC 131 ms
85,380 KB
testcase_19 AC 136 ms
85,716 KB
testcase_20 AC 130 ms
85,376 KB
testcase_21 AC 143 ms
85,388 KB
testcase_22 AC 164 ms
85,476 KB
testcase_23 AC 163 ms
85,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//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;
}
0