結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー maple116maple116
提出日時 2021-08-27 21:29:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 33 ms / 3,000 ms
コード長 4,176 bytes
コンパイル時間 4,493 ms
コンパイル使用メモリ 267,396 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-13 07:56:43
合計ジャッジ時間 5,745 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,384 KB
testcase_02 AC 22 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,388 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 20 ms
4,380 KB
testcase_13 AC 20 ms
4,380 KB
testcase_14 AC 22 ms
4,384 KB
testcase_15 AC 28 ms
4,384 KB
testcase_16 AC 23 ms
4,380 KB
testcase_17 AC 21 ms
4,384 KB
testcase_18 AC 10 ms
4,384 KB
testcase_19 AC 17 ms
4,384 KB
testcase_20 AC 11 ms
4,380 KB
testcase_21 AC 18 ms
4,380 KB
testcase_22 AC 15 ms
4,380 KB
testcase_23 AC 33 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <boost/multiprecision/cpp_int.hpp>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
//g++ hoge.cpp (-std=c++17) -I . で実行
#pragma GCC target("avx")
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
typedef long long ll;
typedef pair<ll,ll> prl;
typedef vector<ll> vcl;
typedef map<ll,ll> mapl;
typedef unordered_map<ll,ll> umap;
#define pb push_back
#define all(v) v.begin(), v.end()
#define rep(i,a,b) for(ll i=a;i<=b;i++)
#define repi(i,a,b) for(int i=a;i<=b;i++)
#define repr(i,a,b) for(ll i=a;i>=b;i--)
#define reps(i,v) for(ll i=0;i<v.size();i++)
#define rept(itr,v) for(auto itr=v.begin();itr!=v.end();itr++)
#define rept1(itr,v) for(auto itr=v.begin()+1;itr!=v.end();itr++)
#define rept2(itr,v) for(auto itr=v.begin();itr!=v.end()-1;itr++)
#define print(x) cout << (x) << endl
#define dprint(x) cerr << (x) << endl
#define fprint(x) printf("%.15lf\n",(x))
#define dfprint(x) cerr << fixed << setprecision(15) << (x) << endl
#define print2(x,y) cout << (x) << " " << (y) << endl
#define dprint2(x,y) cerr << (x) << " " << (y) << endl
#define print3(x,y,z) cout << (x) << " " << (y) << " " << (z) << endl
#define dprint3(x,y,z) cerr << (x) << " " << (y) << " " << (z) << endl
#define YesNo(test) cout << ((test) ? "Yes" : "No") << endl
#define dYesNo(test) cerr << ((test) ? "Yes" : "No") << endl
#define yesno(test) cout << ((test) ? "yes" : "no") << endl
#define dyesno(test) cerr << ((test) ? "yes" : "no") << endl
#define debug cerr << "debug_print" << endl
#define Bint boost::multiprecision::cpp_int
const double mypi = atan2(0.0,-1.0);
template<typename T> void chmin(T &a, const T &b) { a = min(a, b); }
template<typename T> void chmax(T &a, const T &b) { a = max(a, b); }
ll myceil(ll a, ll b) { if(b < 0){ a = -a; b = -b; } return ((a > 0) ? ((a+b-1) / b) : (-(-a / b)) ); }
ll myfloor(ll a, ll b) { if(b < 0){ a = -a; b = -b; } return ((a > 0) ? (a / b) : (-((-a+b-1) / b)) ); }
//const ll mod = 1e9+7;
//const ll mod = 998244353;
//typedef modint1000000007 mint;
//typedef modint998244353 mint;
//typedef modint mint
//cout << sum.val() << endl;
//mint::set_mod(mod); modが固定でないとき

//library borrowed from https://judge.yosupo.jp/submission/53928
//thanks to rich_brain

int isqrt(ll n) {
  return sqrtl(n);
}

__attribute__((target("avx2"), optimize("O3", "unroll-loops")))
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() {
    // your code goes here
    ll l,r,x,y;
    cin >> l >> r;
    x = prime_pi(r) - prime_pi(l-1);
    if(r > l) y = prime_pi(2*r-1) - prime_pi(2*l);
    else y = 0;
    print(x + y);
    return 0;
}
0