結果
問題 | No.2345 max(l,r) |
ユーザー | minimum |
提出日時 | 2023-06-09 23:12:16 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 969 ms / 2,000 ms |
コード長 | 2,488 bytes |
コンパイル時間 | 328 ms |
コンパイル使用メモリ | 82,212 KB |
実行使用メモリ | 134,632 KB |
最終ジャッジ日時 | 2025-01-02 05:14:44 |
合計ジャッジ時間 | 21,667 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 68 |
ソースコード
from itertools import accumulate class Combination: def __init__(self, MX=10**6, MOD=998244353): self.MX = MX self.MOD = MOD self.fact = [1] * (MX + 1) self.inv = [1] * (MX + 1) self.f_inv = [1] * (MX + 1) for i in range(2, MX + 1): self.fact[i] = (self.fact[i - 1] * i) % self.MOD self.inv[i] = ( - (self.MOD // i) * (self.inv[self.MOD % i])) % self.MOD self.f_inv[i] = (self.f_inv[i - 1] * self.inv[i]) % self.MOD def invs(self, n): if n <= self.MX: return self.inv[n] else: return pow(n, self.MOD - 2, self.MOD) def p(self, n, r): if r > n or r < 0: return 0 return (self.fact[n] * self.f_inv[r]) % self.MOD def c(self, n, r): if r > n or r < 0: return 0 return (self.fact[n] * self.f_inv[r] * self.f_inv[n - r]) % self.MOD com = Combination() def solve(): MOD = 998244353 N, M = map(int, input().split()) A = list(map(int, input().split())) dic = dict() for i in range(N): if N - A[i] not in dic: dic[N - A[i]] = 0 dic[N - A[i]] += 1 ls = list(dic.keys()) ls.sort() lsum = 0 rsum = 0 L, R = [], [] LL, RR = [], [] ans = 1 for i in range(len(ls)): now = ls[i] j = now - lsum k = now - rsum if lsum + dic[now] == now: L.append(dic[now]) LL.append(now) lsum = now if rsum + dic[now] == now and i != len(ls) - 1: ans *= 2 ans %= MOD elif rsum + dic[now] == now: R.append(dic[now]) RR.append(now) rsum = now elif j + k == dic[now] and j > 0 and k > 0: lsum = now rsum = now L.append(j) LL.append(now) R.append(k) RR.append(now) ans *= com.c(dic[now], j) ans %= MOD else: print(0) return S = L + list(reversed(R)) checkl = list(accumulate(S)) checkr = list(reversed(list(accumulate(reversed(S))))) checkmin = [min(checkl[i], checkr[i]) for i in range(len(S))] if checkmin != LL + list(reversed(RR)): print(0) else: ans *= com.c(M, len(L) + len(R)) ans %= MOD print(ans) return T = int(input()) for _ in range(T): solve()