結果

問題 No.1555 Constructed Balancing Sequence
ユーザー chineristACchineristAC
提出日時 2021-01-04 03:58:45
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 3,406 bytes
コンパイル時間 694 ms
コンパイル使用メモリ 82,152 KB
実行使用メモリ 768,372 KB
最終ジャッジ日時 2024-06-22 19:26:45
合計ジャッジ時間 4,240 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 MLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
権限があれば一括ダウンロードができます

ソースコード

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(10*K+10)]
    while stack:
        i,j = stack.pop()
        if j in dp[i]:
            continue
        dp[i][j] = [0 for k in range(6*K+2)]
        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))

    cum = [{sum:[0 for minus in range(6*K+2)] for sum in dp[i]} for i in range(N-1)] + [{sum:[0] for sum in dp[N-1]}]

    def cum_construct(i):
        for sum in dp[i]:
            cum[i][sum][0] = dp[i][sum][0]
            for minus in range(1,len(dp[i][sum])):
                cum[i][sum][minus] = dp[i][sum][minus] + cum[i][sum][minus-1]
                cum[i][sum][minus] %= mod

    mod = 998244353

    for minus in range(6*K+2):
        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

    cum_construct(0)

    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(6*K+1,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] += cum[i-1][pre_sum][pre_minus_R] - cum[i-1][pre_sum][pre_minus_L] + dp[i-1][pre_sum][pre_minus_L]
                        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(6*K+1,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] += cum[i-1][pre_sum][pre_minus_R] - cum[i-1][pre_sum][pre_minus_L] + dp[i-1][pre_sum][pre_minus_L]
                        dp[i][sum][minus] %= mod

        cum_construct(i)

    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()))

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