結果

問題 No.1322 Totient Bound
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-12-19 13:03:29
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,364 ms / 5,000 ms
コード長 4,285 bytes
コンパイル時間 1,028 ms
コンパイル使用メモリ 102,644 KB
実行使用メモリ 7,864 KB
最終ジャッジ日時 2024-09-21 10:12:57
合計ジャッジ時間 22,723 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 4 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 242 ms
5,376 KB
testcase_19 AC 389 ms
5,376 KB
testcase_20 AC 975 ms
7,168 KB
testcase_21 AC 1,083 ms
7,424 KB
testcase_22 AC 1,102 ms
7,296 KB
testcase_23 AC 1,364 ms
7,808 KB
testcase_24 AC 1,324 ms
7,680 KB
testcase_25 AC 1,301 ms
7,808 KB
testcase_26 AC 1,302 ms
7,680 KB
testcase_27 AC 1,300 ms
7,808 KB
testcase_28 AC 1,300 ms
7,808 KB
testcase_29 AC 1,300 ms
7,728 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1,305 ms
7,680 KB
testcase_34 AC 1,282 ms
7,864 KB
testcase_35 AC 1,277 ms
7,724 KB
testcase_36 AC 1,297 ms
7,852 KB
testcase_37 AC 1,283 ms
7,728 KB
testcase_38 AC 1,277 ms
7,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


// floor(sqrt(a))
long long floorSqrt(long long a) {
  long long b = a, x = 0, y = 0;
  for (int e = (63 - __builtin_clzll(a)) & ~1; e >= 0; e -= 2) {
    x <<= 1;
    y <<= 1;
    if (b >= (y | 1) << e) {
      b -= (y | 1) << e;
      x |= 1;
      y += 2;
    }
  }
  return x;
}


constexpr int LIM = 100'010;

Int N;
Int sqrtN;
bool isPrime[LIM];
Int primesLen;
Int primes[LIM];
Int small[LIM], large[LIM], large1[LIM];
Int SMALL[LIM], LARGE[LIM];
Int ans;

Int get(Int n) {
  if (n <= sqrtN + 1) return small[n];
  if (n <= N) {
    const Int l = N / n;
    if (n == N / l) return large[l];
  }
  return large1[N / (n - 1)];
}

int main() {
  for (; ~scanf("%lld", &N); ) {
    sqrtN = floorSqrt(N + 1);
    fill(isPrime + 2, isPrime + sqrtN + 1 + 1, true);
    fill(small, small + sqrtN + 1 + 1, 0);
    fill(large, large + sqrtN + 1, 0);
    fill(large1, large1 + sqrtN + 1, 0);
    fill(SMALL, SMALL + sqrtN + 1, 0);
    fill(LARGE, LARGE + sqrtN + 1, 0);
    primesLen = 0;
    for (Int n = 1; n <= sqrtN + 1; ++n) small[n] = n;
    for (Int l = 1; l <= sqrtN; ++l) large[l] = N / l;
    for (Int l = 1; l <= sqrtN; ++l) large1[l] = N / l + 1;
    for (Int p = 2; p <= sqrtN + 1; ++p) {
      if (isPrime[p]) {
        primes[primesLen++] = p;
        const Int p2 = p * p;
        for (Int n = p2; n <= sqrtN + 1; n += p) isPrime[n] = false;
        const Int g1 = small[p - 1];
        for (Int l = 1; l <= sqrtN; ++l) {
          Int n = N / l + 1;
          if (n < p2) break;
          large1[l] -= (get(n / p) - g1);
          --n;
          if (n < p2) break;
          large[l] -= (get(n / p) - g1);
        }
        for (Int n = sqrtN + 1; n >= 1; --n) {
          if (n < p2) break;
          small[n] -= (small[n / p] - g1);
        }
      }
    }
    for (Int n = 1; n <= sqrtN + 1; ++n) small[n] -= 1;
    for (Int l = 1; l <= sqrtN; ++l) large[l] -= 1;
    for (Int l = 1; l <= sqrtN; ++l) large1[l] -= 1;
// cerr<<"sqrtN = "<<sqrtN<<endl;
// cerr<<"primes = ";pv(primes,primes+primesLen);
// cerr<<"small = ";pv(small,small+sqrtN+1+1);
// cerr<<"large = ";pv(large,large+sqrtN+1);
// cerr<<"large1 = ";pv(large1,large1+sqrtN+1);
    
// cerr<<"SMALL = ";pv(SMALL,SMALL+sqrtN+1);
// cerr<<"LARGE = ";pv(LARGE,LARGE+sqrtN+1);
    Int limBef = N + 1;
    for (Int i = primesLen; i--; ) {
      const Int p = primes[i];
      auto GET = [&](Int n) {
        if (n <= sqrtN) {
          return (n >= limBef) ? SMALL[n] : (max(small[n + 1] - (i + 1), 0LL) + 1);
        } else {
          const int l = N / n;
          return (n >= limBef) ? LARGE[l] : (max(large1[l] - (i + 1), 0LL) + 1);
        }
      };
      const Int lim = p * (p - 1);
      for (Int l = 1; l <= sqrtN; ++l) {
        const Int n = N / l;
        if (n < lim) break;
        if (n < limBef) LARGE[l] = GET(n);
        for (Int m = n / (p - 1); m > 0; m /= p) {
          LARGE[l] += GET(m);
        }
      }
      for (Int n = sqrtN; n >= 1; --n) {
        if (n < lim) break;
        if (n < limBef) SMALL[n] = GET(n);
        for (Int m = n / (p - 1); m > 0; m /= p) {
          SMALL[n] += GET(m);
        }
      }
      limBef = lim;
// cerr<<"p = "<<p<<endl;
// cerr<<"SMALL = ";pv(SMALL,SMALL+sqrtN+1);
// cerr<<"LARGE = ";pv(LARGE,LARGE+sqrtN+1);
    }
    const Int ans = (N >= limBef) ? LARGE[1] : (large1[1] + 1);
    printf("%lld\n", ans);
  }
  return 0;
}
0