結果

問題 No.1693 Invasion
ユーザー O2MTO2MT
提出日時 2021-10-01 23:11:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 370 ms / 2,000 ms
コード長 2,613 bytes
コンパイル時間 340 ms
コンパイル使用メモリ 87,108 KB
実行使用メモリ 95,280 KB
最終ジャッジ日時 2023-09-26 22:09:11
合計ジャッジ時間 5,396 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
71,412 KB
testcase_01 AC 68 ms
71,404 KB
testcase_02 AC 78 ms
75,332 KB
testcase_03 AC 72 ms
75,952 KB
testcase_04 AC 68 ms
71,248 KB
testcase_05 AC 79 ms
76,276 KB
testcase_06 AC 81 ms
76,244 KB
testcase_07 AC 79 ms
75,964 KB
testcase_08 AC 80 ms
76,256 KB
testcase_09 AC 262 ms
88,700 KB
testcase_10 AC 216 ms
93,096 KB
testcase_11 AC 143 ms
84,464 KB
testcase_12 AC 233 ms
94,464 KB
testcase_13 AC 157 ms
82,672 KB
testcase_14 AC 162 ms
83,608 KB
testcase_15 AC 134 ms
80,172 KB
testcase_16 AC 177 ms
84,072 KB
testcase_17 AC 68 ms
71,548 KB
testcase_18 AC 319 ms
94,872 KB
testcase_19 AC 143 ms
92,684 KB
testcase_20 AC 77 ms
76,216 KB
testcase_21 AC 339 ms
94,528 KB
testcase_22 AC 337 ms
95,280 KB
testcase_23 AC 370 ms
95,120 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Factorial():
    def __init__(self, mod=998244353):
        self.mod = mod
        self._factorial = [1]
        self._size = 1
        self._factorial_inv = [1]
        self._size_inv = 1
    
    def __call__(self, n):
        '''n! % mod '''
        return self.fact(n)
    
    def fact(self, n):
        '''n! % mod '''
        if n >= self.mod:
            return 0
        self.make(n)
        return self._factorial[n]
    
    def fact_inv(self, n):
        '''n!^-1 % mod '''
        if n >= self.mod:
            raise ValueError('Modinv is not exist! arg={}'.format(n))
        self.make_inv(n)
        return self._factorial_inv[n]
    
    def comb(self, n, r):
        ''' nCr % mod '''
        if r > n:
            return 0
        t = self.fact_inv(n-r)*self.fact_inv(r) % self.mod
        return self(n)*t % self.mod
    
    def comb_with_repetition(self, n, r):
        ''' nHr % mod '''
        t = self.fact_inv(n-1)*self.fact_inv(r) % self.mod
        return self(n+r-1)*t % self.mod
    
    def perm(self, n, r):
        ''' nPr % mod '''
        if r > n:
            return 0
        return self(n)*self.fact_inv(n-r) % self.mod
    
    @staticmethod
    def xgcd(a, b):
        ''' return (g, x, y) such that a*x + b*y = g = gcd(a, b) '''
        x0, x1, y0, y1 = 0, 1, 1, 0
        while a != 0:
            (q, a), b = divmod(b, a), a
            y0, y1 = y1, y0 - q * y1
            x0, x1 = x1, x0 - q * x1
        return b, x0, y0
    
    def modinv(self, n):
        g, x, _ = self.xgcd(n, self.mod)
        if g != 1:
            raise ValueError('Modinv is not exist! arg={}'.format(n))
        return x % self.mod
    
    def make(self, n):
        if n >= self.mod:
            n = self.mod
        if self._size < n+1:
            for i in range(self._size, n+1):
                self._factorial.append(self._factorial[i-1]*i % self.mod)
            self._size = n+1
    
    def make_inv(self, n):
        if n >= self.mod:
            n = self.mod
        self.make(n)
        if self._size_inv < n+1:
            for i in range(self._size_inv, n+1):
                self._factorial_inv.append(self.modinv(self._factorial[i]))
            self._size_inv = n+1


N,M = map(int,input().split())
A = list(map(int,input().split()))
dp = [[False,M] for _ in range(M+1)]
dp[0] = [True,0]
f = Factorial()
for i in range(N):
  for j in range(M-A[i]+1):
    if dp[j][0]:
      dp[j+A[i]][0] = True
      dp[j+A[i]][1] = min(dp[j+A[i]][1],dp[j][1]+1)

ans = 1

for i in range(1,M+1):
  if dp[i][0]:
    ans += f.comb(M-dp[i][1],i-dp[i][1])
    ans %= 998244353

print(ans)
0