結果

問題 No.2457 Stampaholic (Easy)
ユーザー chineristAC
提出日時 2023-09-01 22:36:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,570 ms / 4,000 ms
コード長 1,758 bytes
コンパイル時間 385 ms
コンパイル使用メモリ 81,916 KB
実行使用メモリ 73,028 KB
最終ジャッジ日時 2025-01-03 10:02:02
合計ジャッジ時間 19,441 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys,random
from itertools import permutations
from collections import deque

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

mod = 998244353

def brute(H,W,N,K):
    inv = pow((H-K+1)*(W-K+1),mod-2,mod)


    ans = 0
    for i in range(H):
        for j in range(W):
            """
            h <= i < h+K and 0 <= h < H-K+1
            max(i-K+1,0) <= h <= min(i,H-K)
            """

            h = min(i,H-K) - max(i-K+1,0) + 1
            w = min(j,W-K) - max(j-K+1,0) + 1

            ans += 1 - pow(1-h*w*inv % mod,N,mod)
            ans %= mod

    return ans

def calc_const_h(H,W,N,K,h):
    inv = pow((H-K+1)*(W-K+1),mod-2,mod)
    ans = 0

    cosnt_W = W
    for j in range(K-1):
        w = min(j,W-K) - max(j-K+1,0) + 1
        cosnt_W -= 1
        ans += 1 - pow(1-h*w*inv,N,mod)
        ans %= mod

        if (W-1-j) < K-1:
            continue
        w = min(W-1-j,W-K) - max(W-1-j-K+1,0) + 1
        cosnt_W -= 1
        ans += 1 - pow(1-h*w*inv,N,mod)
        ans %= mod
    
    w = K
    ans += (1 - pow(1-h*w*inv,N,mod) % mod) * cosnt_W % mod
    ans %= mod
    return ans



def solve_easy(H,W,N,K):    
    
    inv = pow((H-K+1)*(W-K+1),mod-2,mod)
    ans = 0

    const_H = H

    for i in range(K-1):
        h = min(i,H-K) - max(i-K+1,0) + 1
        const_H -= 1
        ans += calc_const_h(H,W,N,K,h)
        ans %= mod

        if H-1-i < K-1:
            continue

        h = min(H-1-i,H-K) - max(H-1-i-K+1,0) + 1
        const_H -= 1
        ans += calc_const_h(H,W,N,K,h)
        ans %= mod
    
    h = K
    ans += const_H * calc_const_h(H,W,N,K,h) % mod
    ans %= mod
    
    return ans


    
    


H,W,N,K = mi()

print(solve_easy(H,W,N,K))


0