結果

問題 No.644 G L C C D M
ユーザー maspy
提出日時 2020-03-31 20:35:57
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 688 bytes
コンパイル時間 108 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-06-25 02:08:06
合計ジャッジ時間 2,222 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/ python3.8
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
from functools import lru_cache

N, M = map(int, read().split())
MOD = 10**9 + 7


@lru_cache(None)
def f(N):
    """ count 1 <= a < b <= N s.t. gcd(a,b) == 1"""
    if N <= 1:
        return 0
    ret = N * (N - 1) // 2
    sqN = int(N ** .5)
    for d in range(2, N + 1):
        n = N // d
        if n <= sqN:
            break
        ret -= f(n)
    for n in range(1, sqN + 1):
        cnt = (N // n) - (N // (n + 1))
        ret -= f(n) * cnt
    return ret


x = 2 * f(N // M) % MOD
for i in range(1, N - 1):
    x *= i
    x %= MOD
print(x)
0