結果

問題 No.2576 LCM Pattern
ユーザー LyricalMaestroLyricalMaestro
提出日時 2024-01-06 18:32:07
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 1,567 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 132,928 KB
最終ジャッジ日時 2024-01-06 18:32:13
合計ジャッジ時間 5,473 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
60,392 KB
testcase_01 AC 36 ms
53,588 KB
testcase_02 AC 59 ms
66,696 KB
testcase_03 AC 36 ms
53,588 KB
testcase_04 AC 36 ms
53,588 KB
testcase_05 AC 69 ms
73,768 KB
testcase_06 AC 147 ms
77,528 KB
testcase_07 AC 40 ms
59,540 KB
testcase_08 AC 150 ms
77,528 KB
testcase_09 AC 58 ms
66,560 KB
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

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

MOD = 998244353

import math

def calc_gcd(A, B):
    """
    正の整数A, Bの最大公約数を計算する
    """
    a = max(A, B)
    b = min(A, B)
    while a % b > 0:
        c = a % b
        a = b
        b = c
    return b

def main():
    N, M = map(int, input().split())

    # Mの約数列挙
    sqrt_m = int(math.sqrt(M))
    divisors = []
    for p in range(1, sqrt_m + 1):
        if M % p == 0:
            q = M // p
            divisors.append(p)
            if q != p:
                divisors.append(q)     
    divisors.sort()
    lcm_map = {}
    for d1 in divisors:
        for d2 in divisors:
            gcd = calc_gcd(d1, d2)
            lcm_map[(d1, d2)] = (d1 // gcd) * d2


    def solve(map1, map2, divisors):
        new_map = {}
        for d1 in divisors:
            for d2 in divisors:
                lcm = lcm_map[(d1, d2)]
                if M % lcm != 0:
                    break

                if d1 not in map1 or d2 not in map2:
                    continue
                
                if lcm not in new_map:
                    new_map[lcm] = 0
                new_map[lcm] += (map1[d1] * map2[d2]) % MOD
                new_map[lcm] %= MOD
        return new_map

    answer_map = {1:1}
    base_map = {d: 1 for d in divisors}
    while N > 0:
        if N % 2 == 1:
            answer_map = solve(answer_map, base_map, divisors)


        N //= 2
        base_map = solve(base_map, base_map, divisors)
    print(answer_map[M])


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