結果

問題 No.890 移調の限られた旋法
ユーザー neterukunneterukun
提出日時 2019-09-20 22:53:25
言語 PyPy3
(7.3.13)
結果
WA  
実行時間 -
コード長 1,981 bytes
コンパイル時間 397 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 161,316 KB
最終ジャッジ日時 2023-10-12 20:45:20
合計ジャッジ時間 31,264 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 803 ms
160,576 KB
testcase_01 AC 803 ms
159,936 KB
testcase_02 AC 800 ms
160,128 KB
testcase_03 AC 800 ms
160,624 KB
testcase_04 AC 803 ms
160,664 KB
testcase_05 AC 830 ms
159,972 KB
testcase_06 AC 802 ms
160,692 KB
testcase_07 AC 803 ms
159,984 KB
testcase_08 AC 801 ms
160,592 KB
testcase_09 AC 802 ms
160,328 KB
testcase_10 AC 803 ms
159,796 KB
testcase_11 AC 801 ms
159,960 KB
testcase_12 AC 806 ms
160,332 KB
testcase_13 WA -
testcase_14 AC 800 ms
159,816 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 805 ms
160,536 KB
testcase_21 AC 804 ms
159,800 KB
testcase_22 AC 800 ms
160,072 KB
testcase_23 AC 802 ms
160,108 KB
testcase_24 AC 802 ms
160,460 KB
testcase_25 AC 800 ms
159,776 KB
testcase_26 WA -
testcase_27 AC 801 ms
160,720 KB
testcase_28 AC 800 ms
159,708 KB
testcase_29 AC 799 ms
160,136 KB
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class Combination:
    '''MOD上の
    計算量:階乗・逆元テーブルの作成O(N)
    nCkを求めるO(1)'''

    def __init__(self, n, MOD):
        self.fact = [1]
        for i in range(1, n + 1):
            self.fact.append(self.fact[-1] * i % MOD)
        self.inv_fact = [pow(self.fact[i], MOD - 2, MOD) for i in range(n + 1)]
        self.MOD = MOD

    def factorial(self, k):
        """k!を求める O(1)"""
        return self.fact[k]

    def inverse_factorial(self, k):
        """k!の逆元を求める O(1)"""
        return self.inv_fact[k]

    def permutation(self, k, r):
        """kPrを求める O(1)"""
        if k < r:
            return 0
        return (self.fact[k] * self.inv_fact[r]) % self.MOD

    def combination(self, k, r):
        """kCrを求める O(1)"""
        if k < r:
            return 0
        return (self.fact[k] * self.inv_fact[k - r] * self.inv_fact[r]) % self.MOD


def make_divisors(n):
    """自然数nの約数を列挙したリストを出力する
    計算量: O(sqrt(N))
    入出力例: 12 -> [1, 2, 3, 4, 6, 12]
    """
    divisors = []
    for k in range(1, int(n**0.5) + 1):
        if n % k == 0:
            divisors.append(k)
            if k != n // k:
                divisors.append(n // k)
    divisors = sorted(divisors)
    return divisors


def gcd(a, b):
    """a, bの最大公約数(greatest common divisor:GCD)を求める"""
    if b == 0:
        return a
    return gcd(b, a%b)


n, k = map(int, input().split())
MOD = 10**9 + 7
comb = Combination(10**6 + 10, MOD)

gcd_nk = gcd(n, k)
div_list = make_divisors(gcd_nk)

li = []
for num in div_list:
    if num == 1:
        continue
    total = n // num
    selected = k // num
    tmp = comb.combination(total, selected)
    li.append((total, tmp))

ans = 0
for i in range(len(li)):
    ans += li[i][1]
    for j in range(i):
        if li[j][0] % li[i][0] == 0:
            ans -= li[i][1]
    ans %= MOD
print(ans % MOD)
          
0