結果
| 問題 |
No.2003 Frog on Grid
|
| コンテスト | |
| ユーザー |
gew1fw
|
| 提出日時 | 2025-06-12 14:56:45 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,866 bytes |
| コンパイル時間 | 148 ms |
| コンパイル使用メモリ | 82,388 KB |
| 実行使用メモリ | 78,520 KB |
| 最終ジャッジ日時 | 2025-06-12 14:58:38 |
| 合計ジャッジ時間 | 8,120 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 14 TLE * 1 -- * 11 |
ソースコード
import sys
import bisect
MOD = 998244353
def main():
H, W, K = map(int, sys.stdin.readline().split())
grid = []
for _ in range(H):
line = sys.stdin.readline().strip()
grid.append(line)
# Precompute lines: for each s, list of (x, y) in order of increasing x
max_s = H + W
lines = [[] for _ in range(max_s + 1)]
for x in range(1, H + 1):
for y in range(1, W + 1):
s = x + y
if s >= 2 and s <= H + W:
lines[s].append((x, y))
# Sort each line by x
for s in range(2, max_s + 1):
lines[s].sort()
# Initialize dp and prefix sums
dp = [[0] * (W + 1) for _ in range(H + 1)]
dp[1][1] = 1
# For each line s, we'll maintain a list of x's and a prefix sum array
line_prefix = {} # s' : (list_x, prefix_sum)
# Initialize line s=2
s = 2
list_x = []
prefix_sum = []
for (x, y) in lines[s]:
if x == 1 and y == 1:
list_x.append(x)
prefix_sum.append(1)
else:
list_x.append(x)
new_sum = (prefix_sum[-1] + dp[x][y]) if prefix_sum else dp[x][y]
prefix_sum.append(new_sum)
line_prefix[s] = (list_x, prefix_sum)
for s in range(3, max_s + 1):
if not lines[s]:
continue
list_x = []
prefix_sum = []
current_sum = 0
for (x, y) in lines[s]:
if grid[x-1][y-1] == '#':
dp_val = 0
else:
total = 0
for t in range(1, K + 1):
s_prime = s - t
if s_prime < 2:
break
if s_prime not in line_prefix:
continue
a = max(1, s_prime - y)
b = min(x, s_prime - 1)
if a > b:
continue
# Get the line's x list and prefix sum
x_list, pre_sum = line_prefix[s_prime]
# Find left and right indices
left = bisect.bisect_left(x_list, a)
right = bisect.bisect_right(x_list, b) - 1
if left > right:
continue
if left == 0:
sum_part = pre_sum[right]
else:
sum_part = pre_sum[right] - pre_sum[left - 1]
total += sum_part
if total >= MOD:
total -= MOD
dp_val = total % MOD
dp[x][y] = dp_val
current_sum = (current_sum + dp_val) % MOD
list_x.append(x)
prefix_sum.append(current_sum)
line_prefix[s] = (list_x, prefix_sum)
print(dp[H][W] % MOD)
if __name__ == '__main__':
main()
gew1fw