結果
問題 | No.2457 Stampaholic (Easy) |
ユーザー |
![]() |
提出日時 | 2025-03-26 15:54:08 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 394 ms / 4,000 ms |
コード長 | 1,818 bytes |
コンパイル時間 | 313 ms |
コンパイル使用メモリ | 82,256 KB |
実行使用メモリ | 110,448 KB |
最終ジャッジ日時 | 2025-03-26 15:54:52 |
合計ジャッジ時間 | 4,515 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
ソースコード
import sysfrom collections import defaultdictMOD = 998244353def get_distribution(H, K):dist = defaultdict(int)s = H - K + 1# Case 1: l <= K-1if s >= K - 1:for a in range(1, K):dist[a] += 1else:for a in range(1, s + 1):dist[a] += 1remaining = (K - 1) - sif remaining > 0:dist[s] += remaining# Case 2a: a = Kt = max(0, H - 2 * K + 2)if t > 0:dist[K] += t# Case 2b: a_val from 1 to len_case2bstart = max(K, H - K + 2)end = Hif start <= end:len_case2b = end - start + 1for a in range(1, len_case2b + 1):dist[a] += 1return distdef main():H, W, N, K = map(int, sys.stdin.readline().split())# Compute row distributionrow_dist = get_distribution(H, K)# Compute column distributioncol_dist = get_distribution(W, K)# Compute T and inv_TTh = (H - K + 1) % MODTw = (W - K + 1) % MODT = (Th * Tw) % MODif T == 0:print(0)returninv_T = pow(T, MOD - 2, MOD)# Calculate the total expectationtotal = 0# Precompute pow for each unique c_valpow_cache = {}for a_val, a_cnt in row_dist.items():for b_val, b_cnt in col_dist.items():c_val = a_val * b_valnumerator = (c_val % MOD) * inv_T % MODprob_not = (1 - numerator) % MODif prob_not in pow_cache:pow_val = pow_cache[prob_not]else:pow_val = pow(prob_not, N, MOD)pow_cache[prob_not] = pow_valterm = (1 - pow_val) % MODcnt = (a_cnt * b_cnt) % MODtotal = (total + term * cnt) % MODprint(total % MOD)if __name__ == "__main__":main()