結果

問題 No.1322 Totient Bound
ユーザー Min_25Min_25
提出日時 2020-12-19 12:40:01
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,974 bytes
コンパイル時間 1,054 ms
コンパイル使用メモリ 87,592 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-21 10:11:48
合計ジャッジ時間 7,346 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,940 KB
testcase_09 AC 1 ms
6,944 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 2 ms
6,944 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 2 ms
6,940 KB
testcase_14 AC 3 ms
6,944 KB
testcase_15 AC 2 ms
6,940 KB
testcase_16 AC 2 ms
6,940 KB
testcase_17 AC 2 ms
6,940 KB
testcase_18 AC 58 ms
6,944 KB
testcase_19 AC 91 ms
6,944 KB
testcase_20 AC 241 ms
6,940 KB
testcase_21 AC 264 ms
6,940 KB
testcase_22 AC 269 ms
6,940 KB
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 325 ms
6,940 KB
testcase_29 AC 324 ms
6,940 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,944 KB
testcase_32 AC 2 ms
6,940 KB
testcase_33 AC 327 ms
6,944 KB
testcase_34 AC 322 ms
6,940 KB
testcase_35 WA -
testcase_36 AC 328 ms
6,940 KB
testcase_37 AC 319 ms
6,940 KB
testcase_38 AC 323 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <ctime>
#include <cassert>
#include <cstdlib>
#include <cmath>

#include <algorithm>
#include <map>
#include <vector>
#include <tuple>
#include <array>

#include <functional>

using namespace std;

using u8 = unsigned char;
using i64 = long long;
using u64 = long long unsigned;

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

i64 divide(i64 n, i64 d) {
  return double(n) / d;
}

__attribute__((target("avx"), optimize("O3", "unroll-loops")))
pair< vector<int>, vector<i64> > prime_counts(const i64 N) {
  const int v = isqrt(N);
  vector<int> smalls(v + 1);
  vector<i64> larges(v + 1);
  for (int i = 1; i <= v; ++i) smalls[i] = i - 1;
  for (int i = 1; i <= v; ++i) larges[i] = N / i - 1;
  for (int p = 2, pcnt = 0; p <= v; ++p) if (smalls[p] > smalls[p - 1]) {
    const i64 M = N / p, q = i64(p) * p;
    const int w = v / p, l = min<i64>(v, N / q);
    for (int i = 1; i <= w; ++i) larges[i] -= larges[i * p] - pcnt;
    const int t = min(isqrt(M), l);
    for (int i = w + 1; i <= t; ++i) larges[i] -= smalls[divide(M, i)] - pcnt;
    for (int i = l, j = M / l; i > t; ++j) {
      int c = smalls[j];
      while (j + 1 <= v && smalls[j + 1] == c) ++j;
      c -= pcnt;
      for (int e = max<int>(t, divide(M, j + 1)); i > e; --i) larges[i] -= c;
    }
    for (int i = v, j = v / p; j >= p; --j) {
      const int c = smalls[j] - pcnt;
      for (int e = j * p; i >= e; --i) smalls[i] -= c;
    }
    ++pcnt;
  }
  return make_pair(smalls, larges);
}

using u8 = unsigned char;

vector<int> prime_sieve(int N) {
  if (N <= 1) return vector<int>();
  const int sieve_size = 32 << 10;
  static u8 block[sieve_size];
  const int v = sqrt(N), vv = sqrt(v);
  vector<bool> is_prime(v + 1, 1);
  vector<pair<int, int>> sprimes;
  for (int i = 2; i <= vv; ++i) if (is_prime[i]) {
    for (int j = i * i; j <= v; j += i) is_prime[j] = 0;
  }
  for (int i = 3; i <= v; i += 2) if (is_prime[i]) sprimes.emplace_back(i, i * i / 2);
  const int rsize = N > 60184 ? N / (log(N) - 1.1)
                              : max(1., N / (log(N) - 1.11)) + 1;
  vector<int> primes(1, 2); primes.resize(rsize);
  int psize = 1;
  auto* pblock = block - 1;
  for (int beg = 1; beg < (N + 1) / 2; beg += sieve_size, pblock -= sieve_size) {
    int end = min(beg + sieve_size, (N + 1) / 2);
    fill(block, block + sieve_size, 1);
    for (int i = 0; i < int(sprimes.size()); ++i) {
      int p, next; tie(p, next) = sprimes[i];
      if (p * p > N) break;
      for (; next < end; next += p) pblock[next] = 0;
      sprimes[i].second = next;
    };
    for (int i = beg; i < end; ++i) if (pblock[i]) primes[psize++] = 2 * i + 1;
  }
  assert(psize <= int(primes.size()));
  primes.resize(psize);
  return primes;
}

int main() {
  u64 N;
  while (~scanf("%llu", &N)) {
    const auto pc = prime_counts(N + 1);
    u64 v = isqrt(N + 1);
    auto primes = prime_sieve(v + 10);
    std::vector<int> isp(v + 1, -1);
    auto count = [&] (u64 n) -> u64 {
      if (n <= v) return pc.first[n];
      else {
        size_t i = N / (n - 1); u64 q = (N + 1) / i;
        assert(q <= n && n <= q + 1);
        if (q == n || (n >= 4 && n % 2 == 0)) return pc.second[i];
        if (isp[i] < 0) {
          isp[i] = 1;
          for (size_t j = 1; j < primes.size(); ++j) {
            u64 p = primes[j];
            if (p * p > n) break;
            if (n % p != 0) continue;
            isp[i] = 0;
            break;
          }
        }
        return pc.second[i] + isp[i];
      }
    };

    function<u64(u64, size_t)> rec = [&] (u64 phi, size_t beg) -> u64 {
      u64 ret = count(phi + 1) - beg;
      for (size_t i = beg; i < primes.size(); ++i) {
        u64 p = primes[i];
        if ((p - 1) * p > phi) break;
        u64 nphi = phi / (p - 1);
        while (nphi + 1 > u64(primes[i])) {
          ret += rec(nphi, i + 1) + 1;
          nphi /= p;
        }
      }
      return ret;
    };
    u64 ans = rec(N, 0) + 1;
    printf("%llu\n", ans);
  }
  return 0;
}
0