結果

問題 No.1322 Totient Bound
ユーザー maspymaspy
提出日時 2020-12-19 15:10:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,532 ms / 5,000 ms
コード長 2,703 bytes
コンパイル時間 275 ms
コンパイル使用メモリ 81,836 KB
実行使用メモリ 89,700 KB
最終ジャッジ日時 2023-10-21 09:15:36
合計ジャッジ時間 58,224 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,616 KB
testcase_01 AC 40 ms
53,616 KB
testcase_02 AC 44 ms
53,616 KB
testcase_03 AC 42 ms
53,616 KB
testcase_04 AC 41 ms
53,616 KB
testcase_05 AC 42 ms
53,616 KB
testcase_06 AC 40 ms
53,616 KB
testcase_07 AC 41 ms
53,616 KB
testcase_08 AC 42 ms
53,616 KB
testcase_09 AC 38 ms
53,616 KB
testcase_10 AC 40 ms
53,616 KB
testcase_11 AC 39 ms
53,616 KB
testcase_12 AC 39 ms
53,616 KB
testcase_13 AC 126 ms
76,984 KB
testcase_14 AC 123 ms
76,996 KB
testcase_15 AC 101 ms
76,488 KB
testcase_16 AC 109 ms
76,476 KB
testcase_17 AC 99 ms
76,444 KB
testcase_18 AC 743 ms
79,560 KB
testcase_19 AC 1,075 ms
81,084 KB
testcase_20 AC 2,575 ms
85,488 KB
testcase_21 AC 2,866 ms
86,240 KB
testcase_22 AC 2,904 ms
87,452 KB
testcase_23 AC 3,529 ms
88,392 KB
testcase_24 AC 3,500 ms
89,632 KB
testcase_25 AC 3,498 ms
89,700 KB
testcase_26 AC 3,519 ms
88,400 KB
testcase_27 AC 3,520 ms
89,668 KB
testcase_28 AC 3,532 ms
88,112 KB
testcase_29 AC 3,513 ms
89,688 KB
testcase_30 AC 39 ms
53,404 KB
testcase_31 AC 39 ms
53,616 KB
testcase_32 AC 39 ms
53,616 KB
testcase_33 AC 3,431 ms
88,444 KB
testcase_34 AC 3,429 ms
88,036 KB
testcase_35 AC 3,485 ms
89,500 KB
testcase_36 AC 3,391 ms
87,360 KB
testcase_37 AC 3,404 ms
87,252 KB
testcase_38 AC 3,406 ms
88,144 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def prime_count(N):
    """ O(N^{0/75}) で、 pi(N//i) を列挙する。
    """
    sqN = int(N**.5)
    sum_lo = [0] * (sqN+1)
    sum_hi = [0] * (sqN+1)
    for i in range(1, sqN + 1):
        sum_lo[i] = i - 1
        sum_hi[i] = N // i - 1
    for p in range(2, sqN + 1):
        if sum_lo[p] == sum_lo[p - 1]:
            continue
        sp = sum_lo[p - 1]
        pp = p * p
        for i in range(1, sqN + 1):
            n = N // i
            if n < pp:
                break
            x = sum_hi[i * p] if i * p <= sqN else sum_lo[n // p]
            sum_hi[i] -= x - sp
        for n in range(sqN, pp - 1, -1):
            sum_lo[n] -= sum_lo[n // p] - sp
    return sum_lo, sum_hi
def dfs(N, primes):
    # 値、どの素数まで使ったか
    stack = [(1,1,0)]
    while stack:
        n, phi, nxt_i = stack.pop()
        yield n, phi, nxt_i
        for i in range(nxt_i, len(primes)):
            p = primes[i]
            if phi * (p-1) * p > N:
                break
            phi_p = p - 1
            n_p=p
            while True:
                if phi * phi_p * p > N:
                    break
                stack.append((n*n_p, phi * phi_p, i+1))
                phi_p *= p
                n_p*=p
def main(N):
    pi_lo, pi_hi = prime_count(N)
    sqN = len(pi_lo) - 1
    def pi(x):
        if x <= sqN:
            return pi_lo[x]
        return pi_hi[N//x]
    
    memo = dict()
    def isprime(n):
        if n <= sqN:
            return pi_lo[n] > pi_lo[n-1]
        if n == 2:
            return True
        if n in memo:
            return memo[n]
        if pow(2, n-1, n) != 1:
            memo[n] = False
            return False
        for p in primes:
            if p*p>n:
                break
            if n%p==0:
                memo[n]=False
                return False
        memo[n] = True
        return True
    
    primes = [p for p in range(2, sqN+1) if pi_lo[p] > pi_lo[p-1]]
    if isprime(sqN+1):
        primes.append(sqN+1)
    ans = 1  # N = 1
    for n, phi, nxt_i in dfs(N, primes):
        cnt = 0
        # 2 乗以上をかける場合
        MAX = N // phi
        for i in range(nxt_i, len(primes)):
            p = primes[i]
            k = 1
            phi_p = (p-1)
            while phi_p * p <= MAX:
                k += 1
                phi_p *= p
            if k == 1:
                break
            cnt += k - 1
        # 素数をひとつかける場合。(p-1)phi <= N となる p に関する集計
        # p - 1 <= N//phi。
        k = pi(N//phi)
        if isprime(N//phi+1):
            k += 1
        k -= nxt_i
        cnt += max(k, 0)
        ans += cnt
    return ans

print(main(int(input())))
0