結果

問題 No.2689 Populous
ユーザー 👑 rin204rin204
提出日時 2024-03-07 21:22:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 91 ms / 2,000 ms
コード長 2,196 bytes
コンパイル時間 350 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 75,996 KB
最終ジャッジ日時 2024-03-07 21:22:54
合計ジャッジ時間 3,571 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,460 KB
testcase_01 AC 39 ms
53,460 KB
testcase_02 AC 38 ms
53,460 KB
testcase_03 AC 37 ms
53,460 KB
testcase_04 AC 37 ms
53,460 KB
testcase_05 AC 37 ms
53,460 KB
testcase_06 AC 37 ms
53,460 KB
testcase_07 AC 37 ms
53,460 KB
testcase_08 AC 38 ms
53,460 KB
testcase_09 AC 37 ms
53,460 KB
testcase_10 AC 37 ms
53,460 KB
testcase_11 AC 38 ms
53,460 KB
testcase_12 AC 38 ms
53,460 KB
testcase_13 AC 37 ms
53,460 KB
testcase_14 AC 37 ms
53,460 KB
testcase_15 AC 37 ms
53,460 KB
testcase_16 AC 37 ms
53,460 KB
testcase_17 AC 37 ms
53,460 KB
testcase_18 AC 37 ms
53,460 KB
testcase_19 AC 38 ms
53,460 KB
testcase_20 AC 55 ms
65,796 KB
testcase_21 AC 76 ms
65,796 KB
testcase_22 AC 56 ms
65,796 KB
testcase_23 AC 85 ms
75,832 KB
testcase_24 AC 91 ms
75,936 KB
testcase_25 AC 87 ms
75,996 KB
testcase_26 AC 80 ms
75,596 KB
testcase_27 AC 89 ms
75,924 KB
testcase_28 AC 77 ms
75,568 KB
testcase_29 AC 78 ms
75,568 KB
testcase_30 AC 80 ms
75,568 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