結果

問題 No.2406 Difference of Coordinate Squared
コンテスト
ユーザー LyricalMaestro
提出日時 2026-02-28 13:07:35
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 196 ms / 2,000 ms
コード長 2,838 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 258 ms
コンパイル使用メモリ 78,156 KB
実行使用メモリ 104,536 KB
最終ジャッジ日時 2026-02-28 13:08:08
合計ジャッジ時間 7,518 ms
ジャッジサーバーID
(参考情報)
judge7 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 55
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

import math

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():
    N, M = map(int, input().split())

    combi = CombinationCalculator(2* N, MOD)

    if M != 0:
        # 約数列挙する
        m = abs(M)
        sqrt_m = int(math.sqrt(m))
        base_divisors = []
        for p in range(1, sqrt_m + 1):
            if m % p == 0:
                q = m // p
                base_divisors.append(p)
                if q != p:
                    base_divisors.append(q)
        divisors = []
        for d in base_divisors:
            divisors.append(d)
            divisors.append(-d)
        
        # 答えを求めていく
        answer = 0
        for p in divisors:
            q = M // p
            if (N + p) % 2 == 0 and (N + q) % 2 == 0:
                xu = (N + p) // 2
                xv = (N + q) // 2

                ans = combi.calc_combination(N, xu) * combi.calc_combination(N, xv)
                ans %= MOD
                answer += ans
                answer %= MOD
        inv_4 = pow(4, MOD - 2, MOD)
        answer *= pow(inv_4, N, MOD)
        answer %= MOD

        print(answer)
    else:
        answer = 0
        if N % 2 == 0:
            ans1 = (combi.calc_combination(N, N // 2) * pow(2, N, MOD)) % MOD
            ans2 = (combi.calc_combination(N, N // 2) * pow(2, N, MOD)) % MOD
            ans3 = pow(combi.calc_combination(N, N // 2), 2, MOD)
            answer = (ans1 + ans2) % MOD
            answer -= ans3
            answer %= MOD
        inv_4 = pow(4, MOD - 2, MOD)
        answer *= pow(inv_4, N, MOD)
        answer %= MOD
        print(answer)




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