結果

問題 No.644 G L C C D M
ユーザー lam6er
提出日時 2025-04-15 20:51:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,396 bytes
コンパイル時間 180 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 78,292 KB
最終ジャッジ日時 2025-04-15 20:52:34
合計ジャッジ時間 2,485 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10**9 + 7
max_fact = 10**5
fact = [1] * (max_fact + 2)

for i in range(2, max_fact + 1):
    fact[i] = fact[i-1] * i % MOD

def compute_mobius(K):
    if K < 1:
        return []
    spf = [0] * (K + 1)
    spf[0], spf[1] = 1, 1
    for i in range(2, K + 1):
        if spf[i] == 0:
            spf[i] = i
            for j in range(i * i, K + 1, i):
                if spf[j] == 0:
                    spf[j] = i
    mu = [1] * (K + 1)
    for d in range(2, K + 1):
        current = d
        factors = set()
        is_square_free = True
        while current != 1:
            p = spf[current]
            if p in factors:
                is_square_free = False
                break
            factors.add(p)
            current = current // p
        if not is_square_free:
            mu[d] = 0
        else:
            mu[d] = (-1) ** len(factors)
    return mu

def solve():
    import sys
    input = sys.stdin.read
    N, M = map(int, input().split())
    if M == 0:
        print(0)
        return
    K = N // M
    if K < 2:
        print(0)
        return
    mu = compute_mobius(K)
    S = 0
    for d in range(1, K + 1):
        q = K // d
        term = mu[d] * q * q
        S += term
        S %= MOD
    valid_pairs = (S - 1) % MOD
    if N - 2 < 0 or N - 2 > max_fact:
        print(0)
        return
    ans = valid_pairs * fact[N - 2] % MOD
    print(ans)

solve()
0