結果

問題 No.732 3PrimeCounting
ユーザー yuppe19 😺yuppe19 😺
提出日時 2018-09-10 15:25:30
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 927 ms / 3,000 ms
コード長 2,777 bytes
コンパイル時間 994 ms
コンパイル使用メモリ 91,216 KB
実行使用メモリ 32,240 KB
最終ジャッジ日時 2023-08-28 16:12:20
合計ジャッジ時間 19,151 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 6 ms
4,376 KB
testcase_21 AC 20 ms
4,380 KB
testcase_22 AC 20 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 40 ms
4,544 KB
testcase_26 AC 25 ms
4,380 KB
testcase_27 AC 7 ms
4,376 KB
testcase_28 AC 7 ms
4,376 KB
testcase_29 AC 13 ms
4,376 KB
testcase_30 AC 13 ms
4,380 KB
testcase_31 AC 20 ms
4,380 KB
testcase_32 AC 25 ms
4,376 KB
testcase_33 AC 26 ms
4,380 KB
testcase_34 AC 25 ms
4,380 KB
testcase_35 AC 25 ms
4,380 KB
testcase_36 AC 25 ms
4,380 KB
testcase_37 AC 4 ms
4,376 KB
testcase_38 AC 4 ms
4,380 KB
testcase_39 AC 24 ms
4,376 KB
testcase_40 AC 20 ms
4,376 KB
testcase_41 AC 21 ms
4,376 KB
testcase_42 AC 20 ms
4,380 KB
testcase_43 AC 20 ms
4,376 KB
testcase_44 AC 20 ms
4,376 KB
testcase_45 AC 10 ms
4,376 KB
testcase_46 AC 10 ms
4,380 KB
testcase_47 AC 13 ms
4,380 KB
testcase_48 AC 40 ms
4,504 KB
testcase_49 AC 40 ms
4,520 KB
testcase_50 AC 20 ms
4,380 KB
testcase_51 AC 20 ms
4,380 KB
testcase_52 AC 7 ms
4,376 KB
testcase_53 AC 83 ms
6,104 KB
testcase_54 AC 447 ms
17,664 KB
testcase_55 AC 450 ms
17,740 KB
testcase_56 AC 447 ms
17,716 KB
testcase_57 AC 174 ms
9,404 KB
testcase_58 AC 173 ms
9,164 KB
testcase_59 AC 85 ms
5,956 KB
testcase_60 AC 217 ms
10,424 KB
testcase_61 AC 217 ms
10,532 KB
testcase_62 AC 453 ms
17,980 KB
testcase_63 AC 360 ms
15,432 KB
testcase_64 AC 219 ms
10,592 KB
testcase_65 AC 218 ms
10,636 KB
testcase_66 AC 2 ms
4,376 KB
testcase_67 AC 3 ms
4,376 KB
testcase_68 AC 451 ms
17,728 KB
testcase_69 AC 450 ms
18,024 KB
testcase_70 AC 451 ms
17,692 KB
testcase_71 AC 454 ms
17,940 KB
testcase_72 AC 361 ms
15,332 KB
testcase_73 AC 741 ms
27,480 KB
testcase_74 AC 735 ms
27,592 KB
testcase_75 AC 40 ms
4,704 KB
testcase_76 AC 452 ms
17,800 KB
testcase_77 AC 172 ms
9,396 KB
testcase_78 AC 742 ms
27,160 KB
testcase_79 AC 450 ms
17,756 KB
testcase_80 AC 728 ms
27,152 KB
testcase_81 AC 453 ms
17,696 KB
testcase_82 AC 7 ms
4,380 KB
testcase_83 AC 174 ms
9,188 KB
testcase_84 AC 218 ms
10,460 KB
testcase_85 AC 453 ms
17,564 KB
testcase_86 AC 731 ms
27,224 KB
testcase_87 AC 927 ms
32,072 KB
testcase_88 AC 924 ms
32,240 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cmath>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
using i64 = int64_t;
using u64 = uint64_t;
using u128 = __uint128_t;

constexpr u64 O = 5,
              M = (1ULL << 51) * 509 + 1,
              M2 = -(M - 2),
              R2 = u64(-__uint128_t(M) % M);

inline u64 MR(const __uint128_t &x) {
  u64 y = u64(x >> 64) - u64((__uint128_t(u64(x) * M2) * M) >> 64);
  return i64(y) < 0 ? y + M : y;
}

inline u64 init(const u64 &x) {
  return MR(__uint128_t(x) * R2);
}

inline u64 mod_mul(const u64 &x, const u64 &y, const u64 &unused) {
  return MR(__uint128_t(x) * y);
}

map<pair<u64, u64>, u64> cache;

inline u64 mod_pow(u64 a, u64 n, const u64 &m) {
  auto key = make_pair(a, n);
  if(cache.count(key)) { return cache[key]; }
  u64 res = init(1);
  for(; n; n>>=1) {
    if(n & 1) { res = mod_mul(res, a, m); }
    a = mod_mul(a, a, m);
  }
  return cache[key] = res;
}

void myfmt(vector<u64> &a, bool inv) {
  int n = static_cast<int>(a.size());
  if(n == 1) { return; }
  int m = n / 2;
  vector<u64> a0(m), a1(m);
  for(int i=0, j=0; i<m; ++i) {
    a0[i] = a[j++];
    a1[i] = a[j++];
  }
  myfmt(a0, inv);
  myfmt(a1, inv);
  u64 z = mod_pow(init(O), (M-1)/n, M);
  if(inv) { z = mod_pow(z, M-2, M); }
  u64 pz = init(1);
  for(int i=0; i<n; ++i) {
    a[i] = a0[i%m] + mod_mul(pz, a1[i%m], M);
    if(a[i] >= M) { a[i] -= M; }
    pz = mod_mul(pz, z, M);
  }
}

void fmt(vector<u64> &a) {
  myfmt(a, false);
}

void ifmt(vector<u64> &a) {
  myfmt(a, true);
  int n = static_cast<int>(a.size());
  i64 inv = mod_pow(init(n), M-2, M);
  for(int i=0; i<n; ++i) {
    a[i] = mod_mul(a[i], inv, M);
  }
}

vector<u64> convol(vector<u64> a, vector<u64> b) {
  int n = 1;
  while(n < a.size() + b.size()) { n <<= 1; }
  a.resize(n);
  b.resize(n);
  fmt(a);
  fmt(b);
  vector<u64> c(n);
  for(int i=0; i<n; ++i) {
    c[i] = mod_mul(a[i], b[i], M);
  }
  ifmt(c);
  return c;
}

template <class Int>
vector<bool> sieve(Int n) {
  vector<bool> is_prime(n+1, true);
  is_prime[0] = is_prime[1] = false;
  Int sq = Int(sqrt(n));
  for(Int i=2; i<=sq; ++i) {
    if(is_prime[i]) {
      for(Int j=i*i; j<=n; j+=i) {
        is_prime[j] = false;
      }
    }
  }
  return is_prime;
}

int main(void) {
  int n; scanf("%d", &n);
  vector<bool> is_prime = sieve(n*3+1);
  vector<u64> c1(n+1), d2(n*2+1);
  for(int p=0; p<=n; ++p) {
    if(is_prime[p]) {
      ++c1[p];
      ++d2[p*2];
    }
  }
  for(auto &x : c1) { x = init(x); }
  for(auto &x : d2) { x = init(x); }
  vector<u64> c2 = convol(c1, c1),
              c3 = convol(c2, c1),
              d3 = convol(d2, c1);
  u64 res = 0;
  for(int p=0; p<=n*3; ++p) {
    if(is_prime[p]) {
      res += (MR(c3[p]) - MR(d3[p]) * 3) / 6;
    }
  }
  printf("%lu\n", res);
  return 0;
}
0