結果

問題 No.2689 Populous
ユーザー 👑 rin204rin204
提出日時 2024-03-07 21:22:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 98 ms / 2,000 ms
コード長 2,196 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 82,108 KB
実行使用メモリ 76,628 KB
最終ジャッジ日時 2024-09-29 18:42:04
合計ジャッジ時間 2,908 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
52,352 KB
testcase_01 AC 39 ms
52,608 KB
testcase_02 AC 40 ms
52,480 KB
testcase_03 AC 39 ms
52,608 KB
testcase_04 AC 40 ms
52,480 KB
testcase_05 AC 39 ms
52,736 KB
testcase_06 AC 42 ms
52,352 KB
testcase_07 AC 39 ms
52,708 KB
testcase_08 AC 39 ms
53,120 KB
testcase_09 AC 39 ms
52,736 KB
testcase_10 AC 39 ms
52,400 KB
testcase_11 AC 39 ms
52,608 KB
testcase_12 AC 39 ms
52,736 KB
testcase_13 AC 39 ms
52,736 KB
testcase_14 AC 39 ms
52,608 KB
testcase_15 AC 40 ms
52,480 KB
testcase_16 AC 41 ms
52,608 KB
testcase_17 AC 39 ms
52,480 KB
testcase_18 AC 40 ms
52,608 KB
testcase_19 AC 39 ms
52,736 KB
testcase_20 AC 65 ms
64,384 KB
testcase_21 AC 59 ms
64,512 KB
testcase_22 AC 58 ms
64,512 KB
testcase_23 AC 87 ms
76,252 KB
testcase_24 AC 98 ms
76,492 KB
testcase_25 AC 94 ms
76,368 KB
testcase_26 AC 87 ms
75,792 KB
testcase_27 AC 97 ms
76,628 KB
testcase_28 AC 85 ms
76,268 KB
testcase_29 AC 84 ms
76,288 KB
testcase_30 AC 87 ms
76,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 998244353

h, w = map(int, input().split())
assert 3 <= h <= 1000
assert 3 <= w <= 1000

U = list(map(int, input().split()))
L = [0] * h
R = [0] * h
L[0] = U[0]
R[0] = U[-1]
for i in range(1, h - 1):
    L[i], R[i] = map(int, input().split())
D = list(map(int, input().split()))
L[-1] = D[0]
R[-1] = D[-1]

for i in range(h - 1):
    if abs(L[i] - L[i + 1]) > 1:
        print(-1)
        exit()

    if abs(R[i] - R[i + 1]) > 1:
        print(-1)
        exit()

for i in range(w - 1):
    if abs(U[i] - U[i + 1]) > 1:
        print(-1)
        exit()

    if abs(D[i] - D[i + 1]) > 1:
        print(-1)
        exit()

row = False
for i in range(h):
    d = abs(L[i] - R[i])
    if d > w - 1:
        row = True

col = False
for i in range(w):
    d = abs(U[i] - D[i])
    if d > h - 1:
        col = True

assert not row or not col
if not row and not col:
    print(1)
    exit()

if col:
    L, R, U, D = U, D, L, R
    h, w = w, h

dp = [0] * (w - 1)
dp[-1] = 1

for i in range(1, h - 1):
    ndp = [0] * (w - 1)
    d = abs(L[i] - R[i])
    if d <= w - 1:
        ndp[-1] = sum(dp) % MOD
        dp = ndp
        continue

    uu = abs(L[i] - R[i - 1]) > w
    dd = abs(L[i - 1] - R[i]) > w
    if uu and dd:
        for j in range(w - 2):
            ndp[j] += dp[-1]
            if j != 0:
                ndp[j] += dp[j - 1]
            ndp[j] += dp[j]
            if j != w - 3:
                ndp[j] += dp[j + 1]
            ndp[j] %= MOD
    elif uu:
        for j in range(1, w - 2):
            dp[j] += dp[j - 1]
            dp[j] %= MOD
        for j in range(w - 2):
            ndp[j] += dp[-1]
            if j != w - 3:
                ndp[j] += dp[j + 1]
            else:
                ndp[j] += dp[j]
            ndp[j] %= MOD
    elif dd:
        for j in range(w - 4, -1, -1):
            dp[j] += dp[j + 1]
            dp[j] %= MOD
        for j in range(w - 2):
            ndp[j] += dp[-1]
            if j != 0:
                ndp[j] += dp[j - 1]
            else:
                ndp[j] += dp[j]
            ndp[j] %= MOD
    else:
        tot = sum(dp) % MOD
        ndp = [tot] * (w - 1)
        ndp[-1] = 0

    dp = ndp

ans = sum(dp) % MOD
print(ans)
0