結果

問題 No.3809 Count Bipartite Permutations
コンテスト
ユーザー LyricalMaestro
提出日時 2026-11-29 03:03:09
言語 PyPy3
(8.0.1 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
TLE  
実行時間 -
コード長 2,657 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 90 ms
コンパイル使用メモリ 64,304 KB
実行使用メモリ 137,076 KB
最終ジャッジ日時 2026-11-29 03:03:42
合計ジャッジ時間 32,386 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 24 TLE * 6
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

MOD = 998244353

# ソースコードの出典URL: https://github.com/LyricalMaestro/at_coder_snippets/blob/af67863a2fe7f16cce8325bbc531f89968bfa525/src/conbination.py

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 linear_sum(N):
    return (N * (N + 1)) // 2

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

    numbers = [i + 1 for i in reversed(range(2 * N))]
    combi = CombinationCalculator(N, MOD)

    max_sum = linear_sum(2 * N) - linear_sum(N)
    if max_sum < K:
        print(0)
        return

    dp = {(0, 0): 1}
    for n in numbers:
        new_dp = {}
        for key, value in dp.items():
            not_used_count, used_sum = key

            # 利用しない
            if not_used_count + 1 <= N:
                new_key = (not_used_count + 1, used_sum)
                if new_key not in new_dp:
                    new_dp[new_key] = 0
                new_dp[new_key] += value
                new_dp[new_key] %= MOD

            # 利用する
            if used_sum + n <= K and not_used_count >= 1:
                new_key = (not_used_count - 1, used_sum + n)
                if new_key not in new_dp:
                    new_dp[new_key] = 0
                new_dp[new_key] += (value * not_used_count) % MOD
                new_dp[new_key] %= MOD
        dp = new_dp
    ans = (0, K)
    if ans in dp:
        answer = (dp[ans] * combi.factorial[N]) % MOD
        print(answer)
    else:
        print(0)





    





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