結果

問題 No.1322 Totient Bound
ユーザー Min_25Min_25
提出日時 2020-12-19 13:08:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 335 ms / 5,000 ms
コード長 3,820 bytes
コンパイル時間 998 ms
コンパイル使用メモリ 86,704 KB
実行使用メモリ 5,260 KB
最終ジャッジ日時 2023-10-21 09:09:07
合計ジャッジ時間 7,234 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 1 ms
4,348 KB
testcase_02 AC 1 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 1 ms
4,348 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 1 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 2 ms
4,348 KB
testcase_18 AC 61 ms
4,348 KB
testcase_19 AC 93 ms
4,348 KB
testcase_20 AC 246 ms
4,732 KB
testcase_21 AC 273 ms
4,996 KB
testcase_22 AC 267 ms
4,996 KB
testcase_23 AC 328 ms
5,260 KB
testcase_24 AC 335 ms
5,260 KB
testcase_25 AC 320 ms
5,260 KB
testcase_26 AC 328 ms
5,260 KB
testcase_27 AC 321 ms
5,260 KB
testcase_28 AC 321 ms
5,260 KB
testcase_29 AC 329 ms
5,260 KB
testcase_30 AC 1 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 2 ms
4,348 KB
testcase_33 AC 334 ms
5,260 KB
testcase_34 AC 319 ms
5,260 KB
testcase_35 AC 320 ms
5,260 KB
testcase_36 AC 315 ms
5,260 KB
testcase_37 AC 323 ms
5,260 KB
testcase_38 AC 320 ms
5,260 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)) {
    auto pc = prime_counts(N);
    u64 v = isqrt(N);
    auto primes = prime_sieve(v + 10);
    auto isp = [&] (u64 n) -> bool {
      if (n <= 1) return 0;
      for (size_t j = 0; j < primes.size(); ++j) {
        u64 p = primes[j];
        if (p * p > n) return 1;
        if (n % p == 0) return 0;
      }
      abort();
    };
    for (size_t i = 1; i <= v; ++i) pc.first[i] += isp(i + 1);
    for (size_t i = 1; i <= v; ++i) pc.second[i] += isp(N / i + 1);
    auto count = [&] (u64 n) -> u64 {
      return (n <= v) ? pc.first[n] : pc.second[N / n]; 
    };
    function<u64(u64, size_t)> rec = [&] (u64 phi, size_t beg) -> u64 {
      u64 ret = count(phi) - 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