結果

問題 No.1661 Sum is Prime (Hard Version)
ユーザー Aoxiang CuiAoxiang Cui
提出日時 2021-08-28 00:44:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 5,843 bytes
コンパイル時間 1,365 ms
コンパイル使用メモリ 123,192 KB
実行使用メモリ 6,816 KB
最終ジャッジ日時 2024-11-21 07:04:57
合計ジャッジ時間 8,612 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
5,248 KB
testcase_01 AC 26 ms
5,248 KB
testcase_02 AC 507 ms
5,248 KB
testcase_03 WA -
testcase_04 AC 26 ms
5,248 KB
testcase_05 AC 26 ms
5,248 KB
testcase_06 AC 26 ms
5,248 KB
testcase_07 AC 26 ms
5,248 KB
testcase_08 AC 26 ms
5,248 KB
testcase_09 AC 26 ms
5,248 KB
testcase_10 AC 26 ms
5,248 KB
testcase_11 AC 26 ms
5,248 KB
testcase_12 AC 450 ms
5,248 KB
testcase_13 AC 439 ms
5,248 KB
testcase_14 AC 500 ms
5,248 KB
testcase_15 AC 638 ms
5,248 KB
testcase_16 AC 529 ms
5,248 KB
testcase_17 AC 488 ms
5,248 KB
testcase_18 AC 212 ms
5,248 KB
testcase_19 AC 371 ms
5,248 KB
testcase_20 AC 233 ms
5,248 KB
testcase_21 AC 408 ms
5,248 KB
testcase_22 AC 793 ms
5,248 KB
testcase_23 AC 771 ms
6,816 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define LOCAL
#define _USE_MATH_DEFINES
#include <array>
#include <cassert>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <complex>
#include <cmath>
#include <numeric>
#include <bitset>
#include <functional>
#include <random>
#include <ctime>

using namespace std;

template <typename A, typename B>
ostream& operator <<(ostream& out, const pair<A, B>& a) {
  out << "(" << a.first << "," << a.second << ")";
  return out;
}
template <typename T, size_t N>
ostream& operator <<(ostream& out, const array<T, N>& a) {
  out << "["; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T>
ostream& operator <<(ostream& out, const vector<T>& a) {
  out << "["; bool first = true;
  for (auto v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "]";
  return out;
}
template <typename T, class Cmp>
ostream& operator <<(ostream& out, const set<T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& v : a) { out << (first ? "" : ", "); out << v; first = 0;} out << "}";
  return out;
}
template <typename U, typename T, class Cmp>
ostream& operator <<(ostream& out, const map<U, T, Cmp>& a) {
  out << "{"; bool first = true;
  for (auto& p : a) { out << (first ? "" : ", "); out << p.first << ":" << p.second; first = 0;} out << "}";
  return out;
}
#ifdef LOCAL
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...) 42
#endif
template <typename Arg1>
void __f(const char* name, Arg1&& arg1){
  cerr << name << ": " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args){
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << ": " << arg1 << " |";
  __f(comma + 1, args...);
}

template <class T> auto vect(const T& v, int n) { return vector<T>(n, v); }
template <class T, class... D> auto vect(const T& v, int n, D... m) {
  return vector<decltype(vect(v, m...))>(n, vect(v, m...));
}

typedef long long int64;
typedef pair<int, int> ii;
#define SZ(x) (int)((x).size())
template <typename T> static constexpr T inf = numeric_limits<T>::max() / 2;
const int MOD = 1e9 + 7;
// const int MOD = 998244353;
mt19937 mrand(random_device{}());
int rnd(int x) { return mrand() % x; }
// mt19937_64 mrand(random_device{}());
// int64 rnd(int64 x) { return mrand() % x; }
template <class T> void out(const vector<T>& a) { for (int i = 0; i < SZ(a); ++i) cout << a[i] << " \n"[i + 1 == SZ(a)]; }
template <class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template <class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
template <class T> void dedup(vector<T>& v) { sort(v.begin(), v.end()); v.erase(unique(v.begin(), v.end()), v.end()); }
void add(int& x, int y) { x += y; if (x >= MOD) x -= MOD; }

struct fast_ios {
  fast_ios() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  };
} fast_ios_;

const int R = 1e6 + 10;
const int C = 1e4 + 10;

struct BIT{
  int n; vector<int> val;
  BIT(int _n): n(_n), val(_n + 1, 0) {}
  void add(int i, int x) { for(++i; i <= n; i += i & -i) val[i] += x;}
  int sum(int i) { int ret = 0; for(++i; i; i -= i & -i) ret += val[i]; return ret; }
};

bitset<R> isp;
vector<int> ps, cs;

void init(int n) {
  ps.clear(); cs.clear();
  isp[0] = isp[1] = 1;
  for (int p = 2; p * p <= R; ++p) {
    if (!isp[p]) {
      for (int q = p * p; q <= R; q += p) isp[q] = 1;
    }
  }
  for (int i = 2; i <= R; ++i) {
    if (!isp[i]) {
      ps.push_back(i);
      if (i <= n) cs.push_back(i);
    }
  }
}

int64 phi(int64 x, int64 a, int64 cnt) {
  int64 ret = 0;
  vector<int> mu(a + 1, 1), minp(a + 1, a);
  for (int i = 1; i <= a; ++i) {
    if (!isp[i]) {
      for (int64 j = i; j <= a; j += i) {
        mu[j] *= -1;
        ckmin(minp[j], i);
      }
      for (int64 j = i * i, k = j; k <= a; k += j) mu[k] = 0;
    }
    ret += mu[i] * (x / i);
  }
  vector<int64> sum(cnt, 0);
  for (int64 low = 1; low < x / a; low += a) {
    int64 high = min(low + a, x / a);
    BIT bit(a); bitset<C> is_one;
    for (int b = 0; b < cnt; ++b) {
      int p = cs[b], mn = max(x / p / high, a / p), mx = min(x / p / low, a);
      if (p < mx) {
        for (int m = mx; m > mn; --m) {
          if (mu[m] && minp[m] > p) {
            ret -= mu[m] * (sum[b] + x / p / m - low + 1 - bit.sum(x / p / m - low));
          }
        }
      }
      sum[b] += a - bit.sum(a - 1);
      for (int q = (p - low % p) % p; q < a; q += p) {
        if (!is_one[q]) {
          bit.add(q, 1);
          is_one[q] = 1;
        }
      }
    }
  }
  return ret;
}

int64 pi(int64 x) {
  int r = sqrt(1.0 * x), c = cbrt(x);
  init(c);
  if (x <= R) return upper_bound(ps.begin(), ps.end(), x) - ps.begin();
  int64 a = upper_bound(ps.begin(), ps.end(), c) - ps.begin(),
    b = upper_bound(ps.begin(), ps.end(), r) - ps.begin();
  int64 ret = phi(x, c, a) + (b + a - 2) * (b - a + 1) / 2;
  int idx = b - 1;
  for (int s = r; s <= x && idx >= a; s += c) {
    vector<int64> cur(c + 1);
    bitset<C> val;
    val.flip();
    cur[0] = b;
    for (int p : cs) {
      for (int q = (p - s % p) % p; q <= c; q += p) val[q] = 0;
    }
    for (int i = 1; i <= c; ++i) cur[i] = cur[i - 1] + val[i];
    b = cur[c];
    while (s <= x / ps[idx] && x / ps[idx] < s + c && idx >= a) {
      ret -= cur[x / ps[idx] - s];
      --idx;
    }
  }
  return ret;
}

int main() {
  int64 L, R;
  cin >> L >> R;
  int64 ret = pi(R) - pi(L - 1);
  ret += pi(2 * R - 1) - pi(2 * L);
  cout << ret << '\n';
  return 0;
}
0