結果

問題 No.2829 GCD Divination
ユーザー flippergo
提出日時 2025-04-14 09:58:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,387 ms / 2,000 ms
コード長 743 bytes
コンパイル時間 565 ms
コンパイル使用メモリ 82,772 KB
実行使用メモリ 145,296 KB
最終ジャッジ日時 2025-04-14 09:59:25
合計ジャッジ時間 20,103 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
P = list(range(N+1))
for i in range(2,N+1):
    if i*i>N:break
    for j in range(i*i,N+1,i):
        P[j] = P[i]
def phi(n):
    C = {}
    x = n
    while x>1:
        p = P[x]
        C[p] = C.get(p,0)+1
        x //= p
    ret = n
    for p in C:
        ret *= (1-1/p)
    return ret
memo = {}
def dp(x):
    if x==1:return 0
    if P[x]==x:return x/(x-1)
    if x in memo:return memo[x]
    cnt = 0
    for i in range(2,x):
        if i*i>x:break
        if x%i==0:
            a = i
            b = x//i
            if a!=b:
                cnt += phi(x//a)*dp(a)
                cnt += phi(x//b)*dp(b)
            else:
                cnt += phi(x//a)*dp(a)
    memo[x] = (cnt+x)/(x-1)
    return memo[x]
print(dp(N))
0