結果

問題 No.1555 Constructed Balancing Sequence
ユーザー chineristACchineristAC
提出日時 2021-01-05 01:50:13
言語 PyPy3
(7.3.15)
結果
RE  
(最新)
AC  
(最初)
実行時間 -
コード長 3,304 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 82,368 KB
実行使用メモリ 114,196 KB
最終ジャッジ日時 2024-06-22 19:27:11
合計ジャッジ時間 5,695 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,200 KB
testcase_01 AC 812 ms
114,196 KB
testcase_02 AC 117 ms
77,572 KB
testcase_03 AC 109 ms
76,928 KB
testcase_04 AC 112 ms
76,832 KB
testcase_05 AC 116 ms
77,212 KB
testcase_06 AC 99 ms
76,188 KB
testcase_07 AC 62 ms
68,100 KB
testcase_08 AC 68 ms
71,008 KB
testcase_09 AC 77 ms
74,848 KB
testcase_10 AC 59 ms
67,044 KB
testcase_11 AC 84 ms
77,168 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 RE -
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 RE -
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
testcase_31 RE -
testcase_32 RE -
testcase_33 RE -
testcase_34 RE -
testcase_35 RE -
testcase_36 RE -
testcase_37 RE -
testcase_38 AC 39 ms
53,812 KB
testcase_39 AC 38 ms
53,444 KB
testcase_40 AC 45 ms
60,948 KB
testcase_41 RE -
testcase_42 RE -
testcase_43 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

def solve(N,K,A):
    diff = [A[0] for i in range(N)]
    S = A[0]
    for i in range(1,N):
        diff[i] = S - A[i]
        if diff[i] < 0:
            return 0
        S += A[i]

    diff.append(0)

    dp = [{} for i in range(N-1)] + [{real_S-diff[N]+2*K+1+0:[0] for real_S in range(-2*K,N*K+1)}]
    stack = [(N-2,dp_S) for dp_S in range(K-10,3*K+10)]
    while stack:
        i,j = stack.pop()
        if j in dp[i]:
            continue
        dp[i][j] = [0 for k in range(3*K+1)]
        if not i:
            continue
        if diff[i]:
            stack.append((i-1,(j+diff[i+1]-2*K-1+diff[i])//2-diff[i]+2*K+1))
        if diff[i]<=1:
            for k in range(-2,1):
                stack.append((i-1,(j+diff[i+1]-2*K-1+k)//2+2*K+1))
            stack.append((i-1,(j+diff[i+1]-2*K-1-diff[i])//2+2*K+1))

    mod = 998244353

    for minus in range(3*K+1):
        for sum in dp[0]:
            dp_S = sum - minus
            real_S = diff[1] + dp_S - 2*K - 1
            first = diff[0] - minus
            if first==real_S and -K<=first<=K:
                dp[0][sum][minus] = 1
            if minus:
                dp[0][sum][minus] += dp[0][sum][minus-1]
                dp[0][sum][minus] %= mod

    for i in range(1,N):
        for sum in dp[i]:
            for minus in range(len(dp[i][sum])):
                dp_S = sum - minus
                real_S = dp_S + diff[i+1] - 2*K - 1

                if diff[i]:
                    L = max((real_S+minus+diff[i]+1)//2,-K+minus+diff[i])
                    R = min((real_S+minus+diff[i])//2,K+minus+diff[i])
                    if L==R:
                        pre_dp_S = L - diff[i] + 2*K + 1
                        dp[i][sum][minus] += dp[i-1][pre_dp_S][0]
                        dp[i][sum][minus] %= mod

                if minus%2==diff[i] and real_S%2==0 and -K<=real_S//2<=K:
                    pre_minus_L = max(0,minus//2+1+diff[i])
                    pre_minus_R = min(3*K,minus//2+3*K+diff[i])
                    if pre_minus_L<=pre_minus_R:
                        pre_sum = real_S//2+minus//2+2*K+1
                        dp[i][sum][minus] += dp[i-1][pre_sum][pre_minus_R] - dp[i-1][pre_sum][pre_minus_L-1] * (pre_minus_L>0)
                        dp[i][sum][minus] %= mod

                if diff[i]<=1 and (real_S+minus-diff[i])%2==0:
                    m = max(0,-K+minus-diff[i]-(real_S+minus-diff[i])//2)
                    M = min((minus-diff[i])//2,K+minus-diff[i]-(real_S+minus-diff[i])//2)
                    pre_minus_L = max(0,m+diff[i])
                    pre_minus_R = min(3*K,M+diff[i])
                    if pre_minus_R>=pre_minus_L:
                        pre_sum = (real_S+minus-diff[i])//2+2*K+1
                        dp[i][sum][minus] += dp[i-1][pre_sum][pre_minus_R] - dp[i-1][pre_sum][pre_minus_L-1] * (pre_minus_L>0)
                        dp[i][sum][minus] %= mod

                if minus:
                    dp[i][sum][minus] += dp[i][sum][minus-1]
                    dp[i][sum][minus] %= mod

    res = 0
    for dp_S in dp[N-1]:
        res += dp[N-1][dp_S][0]
        res %= mod
    return res

N,K = map(int,input().split())
A = list(map(int,input().split()))

assert 2<=N<=400
assert 1<=K<=600
assert len(A)==N
for i in range(N):
    assert -K<=A[i]<=K

print(solve(N,K,A))
0