結果

問題 No.1322 Totient Bound
ユーザー 👑 hos.lyrichos.lyric
提出日時 2020-12-19 00:39:33
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 1,245 ms / 5,000 ms
コード長 3,779 bytes
コンパイル時間 715 ms
コンパイル使用メモリ 109,352 KB
実行使用メモリ 6,228 KB
最終ジャッジ日時 2023-09-04 11:36:00
合計ジャッジ時間 22,556 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,380 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,380 KB
testcase_18 AC 220 ms
4,376 KB
testcase_19 AC 345 ms
4,380 KB
testcase_20 AC 917 ms
5,952 KB
testcase_21 AC 1,023 ms
6,156 KB
testcase_22 AC 1,025 ms
4,892 KB
testcase_23 AC 1,242 ms
5,464 KB
testcase_24 AC 1,244 ms
5,084 KB
testcase_25 AC 1,244 ms
6,148 KB
testcase_26 AC 1,234 ms
5,740 KB
testcase_27 AC 1,241 ms
6,228 KB
testcase_28 AC 1,245 ms
6,144 KB
testcase_29 AC 1,238 ms
5,680 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 1 ms
4,376 KB
testcase_33 AC 1,238 ms
6,208 KB
testcase_34 AC 1,233 ms
5,944 KB
testcase_35 AC 1,237 ms
6,156 KB
testcase_36 AC 1,225 ms
5,104 KB
testcase_37 AC 1,223 ms
5,092 KB
testcase_38 AC 1,240 ms
5,152 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.conv, std.functional, std.range, std.stdio, std.string;
import std.algorithm, std.array, std.bigint, std.bitmanip, std.complex, std.container, std.math, std.mathspecial, std.numeric, std.regex, std.typecons;
import core.bitop;

class EOFException : Throwable { this() { super("EOF"); } }
string[] tokens;
string readToken() { for (; tokens.empty; ) { if (stdin.eof) { throw new EOFException; } tokens = readln.split; } auto token = tokens.front; tokens.popFront; return token; }
int readInt() { return readToken.to!int; }
long readLong() { return readToken.to!long; }
real readReal() { return readToken.to!real; }

bool chmin(T)(ref T t, in T f) { if (t > f) { t = f; return true; } else { return false; } }
bool chmax(T)(ref T t, in T f) { if (t < f) { t = f; return true; } else { return false; } }

int binarySearch(alias pred, T)(in T[] as) { int lo = -1, hi = cast(int)(as.length); for (; lo + 1 < hi; ) { const mid = (lo + hi) >> 1; (unaryFun!pred(as[mid]) ? hi : lo) = mid; } return hi; }
int lowerBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a >= val)); }
int upperBound(T)(in T[] as, T val) { return as.binarySearch!(a => (a > val)); }


// floor(sqrt(a))
long floorSqrt(long a) {
  import core.bitop : bsr;
  import std.algorithm : min;
  long b = a, x = 0, y = 0;
  for (int e = bsr(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;
}


long N;
long sqrtN;
bool[] isPrime;
int primesLen;
long[] primes;
long[] small, large, large1;
long ans;

long get(long n) {
  if (n <= sqrtN + 1) return small[n];
  if (n <= N && n == N / (N / n)) return large[N / n];
  if (n <= N + 1 && n == N / (N / (n - 1)) + 1) return large1[N / (n - 1)];
  assert(false);
}

void init() {
  sqrtN = floorSqrt(N + 1);
  isPrime = new bool[sqrtN + 1 + 1];
  small = new long[sqrtN + 1 + 1];
  large = new long[sqrtN + 1];
  large1 = new long[sqrtN + 1];
  isPrime[2 .. $] = true;
  primes = [];
  foreach (n; 1 .. sqrtN + 1 + 1) small[n] = n;
  foreach (l; 1 .. sqrtN + 1) large[l] = N / l;
  foreach (l; 1 .. sqrtN + 1) large1[l] = N / l + 1;
  foreach (p; 2 .. sqrtN + 1 + 1) {
    if (isPrime[p]) {
      primes ~= p;
      for (long n = p^^2; n <= sqrtN + 1; n += p) isPrime[n] = false;
      const g1 = get(p - 1);
      foreach (l; 1 .. sqrtN + 1) {
        {
          const n = N / l + 1;
          if (n < p^^2) break;
          large1[l] -= (get(n / p) - g1);
        }
        {
          const n = N / l;
          if (n < p^^2) break;
          large[l] -= (get(n / p) - g1);
        }
      }
      foreach_reverse (n; 1 .. sqrtN + 1 + 1) {
        if (n < p^^2) break;
        small[n] -= (get(n / p) - g1);
      }
    }
  }
  primesLen = cast(int)(primes.length);
  small[1 .. $] -= 1;
  large[1 .. $] -= 1;
  large1[1 .. $] -= 1;
  debug {
    writeln("sqrtN = ", sqrtN);
    writeln("primes = ", primes);
    writeln("small = ", small);
    writeln("large = ", large);
    writeln("large1 = ", large1);
    stdout.flush;
  }
}

void dfs(int pos, long n) {
  // primes[i] <= n
  if (pos >= 0) {
    debug {
      writefln("dfs %s %s: +1", pos, n);
    }
    ans += 1;
  }
  // primes[i] - 1 <= n
  debug {
    writefln("dfs %s %s: +(pi(%s) - %s)", pos, n, n + 1, pos + 1);
  }
  ans += max(get(n + 1) - (pos + 1), 0);
  if (pos >= 0) {
    const nn = n / primes[pos];
    if (primes[pos] <= nn) {
      dfs(pos, nn);
    }
  }
  foreach (i; pos + 1 .. primesLen) {
    const nn = n / (primes[i] - 1);
    if (primes[i] > nn) {
      break;
    }
    dfs(i, nn);
  }
}

void main() {
  try {
    for (; ; ) {
      N = readLong();
      
      init;
      ans = 1;
      dfs(-1, N);
      writeln(ans);
    }
  } catch (EOFException e) {
  }
}
0