結果

問題 No.644 G L C C D M
ユーザー convexineqconvexineq
提出日時 2021-03-03 20:32:09
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 1,360 bytes
コンパイル時間 322 ms
コンパイル使用メモリ 82,472 KB
実行使用メモリ 67,404 KB
最終ジャッジ日時 2024-10-03 09:59:21
合計ジャッジ時間 2,460 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 22 RE * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

def sum_of_totient(N): 
    R = int(N**0.64)

    # R+1 まで s(i) を前計算
    primes = Eratosthenes(R+1)
    res = list(range(R+1))
    for p in primes:
        for j in range(R//p,0,-1):
            res[j*p] -= res[j]
    from itertools import accumulate
    res = list(accumulate(res))
    # 最終的に、memo[i] = sum_of_totient(N//i)
    S = N//R
    memo = [0]*(S+1)    
    for i in range(S,0,-1):
        x = N//i
        rx = int(x**0.5)
        ans = x*(x+1)//2
        # 漸化式 \sum(s(N/i)) = n*(n+1)//2 を使う
        # floor(N/i) = c となる部分をまとめて計算
        #print(ans)
        for c in range(1,rx):
            ans -= (x//c - x//(c+1))*res[c]
        # そうじゃない部分を個別に計算
        # N//i//j = N//(i*j)
        for j in range(2,x//rx+1):
            if i*j <= S: ans -= memo[i*j]
            else: ans -= res[x//j]

        memo[i] = ans
    return memo[1]

def Eratosthenes(N): #N以下の素数のリストを返す
    N+=1
    is_prime_list = [True]*N
    m = int(N**0.5)+1
    for i in range(3,m,2):
        if is_prime_list[i]:
            is_prime_list[i*i::2*i]=[False]*((N-i*i-1)//(2*i)+1)
    return [2] + [i for i in range(3,N,2) if is_prime_list[i]]

n,m = map(int,input().split())
ans = 2*(sum_of_totient(n//m)-1)
for i in range(n-2):
    ans = ans*(i+1)%1000000007
print(ans)
0