結果
| 問題 |
No.890 移調の限られた旋法
|
| コンテスト | |
| ユーザー |
lam6er
|
| 提出日時 | 2025-03-26 15:48:52 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 65 ms / 2,000 ms |
| コード長 | 2,409 bytes |
| コンパイル時間 | 161 ms |
| コンパイル使用メモリ | 82,376 KB |
| 実行使用メモリ | 76,400 KB |
| 最終ジャッジ日時 | 2025-03-26 15:49:55 |
| 合計ジャッジ時間 | 3,183 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 32 |
ソースコード
MOD = 10**9 + 7
def main():
import sys
N, K = map(int, sys.stdin.readline().split())
# Precompute factorial and inverse factorial modulo MOD
max_n = N
fact = [1] * (max_n + 1)
for i in range(1, max_n + 1):
fact[i] = fact[i-1] * i % MOD
inv_fact = [1] * (max_n + 1)
inv_fact[max_n] = pow(fact[max_n], MOD-2, MOD)
for i in range(max_n-1, -1, -1):
inv_fact[i] = inv_fact[i+1] * (i+1) % MOD
def comb(n, k):
if n < 0 or k < 0 or k > n:
return 0
return fact[n] * inv_fact[k] % MOD * inv_fact[n - k] % MOD
# Find all divisors d > 1 of N
divisors = []
n = N
i = 2
while i * i <= n:
if n % i == 0:
divisors.append(i)
while n % i == 0:
n //= i
i += 1
if n > 1:
divisors.append(n)
# Function to generate all divisors >1 using the prime factors
from itertools import product
primes = {}
n = N
for p in divisors:
cnt = 0
while n % p == 0:
cnt += 1
n //= p
primes[p] = cnt
# Regenerate the list of unique prime factors
unique_primes = list(primes.keys())
# Generate all divisors >1
all_d = [1]
for p in unique_primes:
exponents = [p**e for e in range(1, primes[p]+1)]
new_divs = []
for d in all_d:
for exp in exponents:
new_divs.append(d * exp)
all_d += new_divs
all_d = list(set(all_d))
all_d = [d for d in all_d if d > 1]
total = 0
for d in all_d:
if K % d != 0:
continue
# Compute mu(d)
mu = 1
temp = d
for p in unique_primes:
if temp % p == 0:
cnt = 0
while temp % p == 0:
cnt += 1
temp //= p
if cnt >= 2:
mu = 0
break
mu *= -1
if temp != 1:
# d has a prime factor not in primes, which is impossible
pass
if mu == 0:
continue
# Compute combination
n_div = N // d
k_div = K // d
c = comb(n_div, k_div)
total += mu * c
total %= MOD
# The answer is (-total) mod MOD
ans = (-total) % MOD
print(ans)
if __name__ == '__main__':
main()
lam6er