結果

問題 No.2896 Monotonic Prime Factors
ユーザー LyricalMaestro
提出日時 2025-07-13 00:36:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 723 ms / 2,000 ms
コード長 2,070 bytes
コンパイル時間 384 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 239,092 KB
最終ジャッジ日時 2025-07-13 00:37:09
合計ジャッジ時間 16,696 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 18
権限があれば一括ダウンロードができます

ソースコード

diff #

## https://yukicoder.me/problems/no/3072

MOD = 998244353

class CombinationCalculator:
    """
    modを考慮したPermutation, Combinationを計算するためのクラス
    """    
    def __init__(self, size, mod):
        self.mod = mod
        self.factorial = [0] * (size + 1)
        self.factorial[0] = 1
        for i in range(1, size + 1):
            self.factorial[i] = (i * self.factorial[i - 1]) % self.mod
        
        self.inv_factorial = [0] * (size + 1)
        self.inv_factorial[size] = pow(self.factorial[size], self.mod - 2, self.mod)

        for i in reversed(range(size)):
            self.inv_factorial[i] = ((i + 1) * self.inv_factorial[i + 1]) % self.mod

    def calc_combination(self, n, r):
        if n < 0 or n < r or r < 0:
            return 0

        if r == 0 or n == r:
            return 1
        
        ans = self.inv_factorial[n - r] * self.inv_factorial[r]
        ans %= self.mod
        ans *= self.factorial[n]
        ans %= self.mod
        return ans
    
    def calc_permutation(self, n, r):
        if n < 0 or n < r:
            return 0

        ans = self.inv_factorial[n - r]
        ans *= self.factorial[n]
        ans %= self.mod
        return ans
        

def main():
    Q = int(input())
    ab = []
    a_max = 1
    for _ in range(Q):
        a, b = map(int, input().split())
        ab.append((a , b))
        a_max = max(a_max, a)

    combi = CombinationCalculator(10 ** 7, MOD)

    # osa-k法で素因数分解
    primes = [i for i in range(a_max + 1)]
    for p in range(2, a_max + 1):
        if primes[p] != p:
            continue

        x = 2 * p
        while x <= a_max:
            if primes[x] == x:
                primes[x] = p
            x += p

    total_prime_num = 0
    for a, b in ab:
        prime_num = 0
        while a > 1:
            p = primes[a]
            a //= p
            prime_num += 1
        total_prime_num += prime_num

        ans = combi.calc_combination(total_prime_num - 1, b - 1)
        print(ans)







if __name__ == "__main__":
    main()
0