結果

問題 No.2249 GCDistance
ユーザー tamatotamato
提出日時 2023-03-17 22:24:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,800 ms / 5,000 ms
コード長 658 bytes
コンパイル時間 165 ms
コンパイル使用メモリ 81,728 KB
実行使用メモリ 232,244 KB
最終ジャッジ日時 2023-10-18 15:21:52
合計ジャッジ時間 34,993 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,688 ms
231,680 KB
testcase_01 AC 2,783 ms
232,232 KB
testcase_02 AC 2,778 ms
232,232 KB
testcase_03 AC 2,792 ms
232,236 KB
testcase_04 AC 2,722 ms
232,084 KB
testcase_05 AC 2,773 ms
232,244 KB
testcase_06 AC 2,777 ms
232,228 KB
testcase_07 AC 2,795 ms
232,232 KB
testcase_08 AC 2,681 ms
232,040 KB
testcase_09 AC 2,800 ms
232,240 KB
testcase_10 AC 2,753 ms
232,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
mod = 998244353

NN = 10 ** 7 + 1
ans = [0] * (NN + 1)
min_factor = list(range(NN + 1))
for i in range(2, NN + 1):
    if min_factor[i] == i:
        for x in range(2, NN + 1):
            if x * i > NN:
                break
            min_factor[x * i] = min(min_factor[x * i], i)
for i in range(2, NN + 1):
    ii = i
    prev_p = 1
    phi = i
    while ii != 1:
        p = min_factor[ii]
        if p != prev_p:
            prev_p = p
            phi *= p - 1
            phi //= p
        ii //= p
    ans[i] = ans[i - 1] + 2 * (i - 1) - phi

for _ in range(int(input())):
    N = int(input())
    print(ans[N])
0