結果

問題 No.2792 Security Cameras on Young Diagram
コンテスト
ユーザー LyricalMaestro
提出日時 2026-08-24 00:44:52
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 119 ms / 2,000 ms
+ 114µs
コード長 2,682 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 240 ms
コンパイル使用メモリ 95,728 KB
実行使用メモリ 112,256 KB
最終ジャッジ日時 2026-08-24 00:44:58
合計ジャッジ時間 3,995 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

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 = int(input())
    A = list(map(int ,input().split()))

    a_map = {}
    for a in A:
        if a not in a_map:
            a_map[a] = 0
        a_map[a] += 1
    a_array = [(a, v) for a, v in a_map.items()]
    a_array.sort(key=lambda x : x[0], reverse=True)

    combi = CombinationCalculator(N + max(A), MOD)
    answer = 0
    height = 0
    for index in range(len(a_array)):
        if index == 0:
            start = a_array[index][0]
            height += a_array[index][1]
            end  =0
            if index < len(a_array) - 1:
                end = a_array[index + 1][0]

            answer += combi.calc_combination(start + height - 1, start)
            answer %= MOD
            for j in reversed(range(end, start)):
                answer += combi.calc_combination(j + height - 1, j)
                answer %= MOD
        else:
            start = a_array[index][0]
            end = 0
            if index < len(a_array) - 1:
                end = a_array[index + 1][0]

            for h in range(height, height + a_array[index][1]):
                answer += combi.calc_combination(start - 1 + h, start - 1)
                answer %= MOD

            height += a_array[index][1]
            for j in reversed(range(end, start)):
                answer += combi.calc_combination(j + height - 1, j)
                answer %= MOD
    print(answer)

            




    





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