結果

問題 No.1555 Constructed Balancing Sequence
ユーザー chineristACchineristAC
提出日時 2021-01-04 16:57:03
言語 PyPy3
(7.3.15)
結果
MLE  
(最新)
AC  
(最初)
実行時間 -
コード長 4,687 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,260 KB
実行使用メモリ 848,496 KB
最終ジャッジ日時 2024-06-22 19:27:31
合計ジャッジ時間 5,147 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,172 KB
testcase_01 AC 1,757 ms
218,520 KB
testcase_02 AC 108 ms
77,244 KB
testcase_03 AC 107 ms
76,780 KB
testcase_04 AC 128 ms
77,476 KB
testcase_05 AC 110 ms
76,984 KB
testcase_06 AC 99 ms
76,392 KB
testcase_07 AC 68 ms
72,104 KB
testcase_08 AC 82 ms
76,744 KB
testcase_09 AC 99 ms
77,408 KB
testcase_10 AC 65 ms
70,072 KB
testcase_11 AC 108 ms
77,856 KB
testcase_12 MLE -
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)]
    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(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)]

    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

    for sum in dp[0]:
        cum[0][sum][0] = dp[0][sum][0]
        for minus in range(1,6*K+2):
            cum[0][sum][minus] = dp[0][sum][minus] + cum[0][sum][minus-1]
            cum[0][sum][minus] %= mod

    for i in range(1,N-1):
        for minus in range(6*K+2):
            for sum in dp[i]:
                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

        for sum in cum[i]:
            cum[i][sum][0] = dp[i][sum][0]
            for minus in range(1,6*K+2):
                cum[i][sum][minus] = cum[i][sum][minus-1] + dp[i][sum][minus]
                cum[i][sum][minus] %= mod


    res = 0
    for real_S in range(-N*K,N*K+1):
        minus = 0
        i = N - 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
                res += dp[i-1][pre_dp_S][0]
                res %= 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
                res += cum[i-1][pre_sum][pre_minus_R] - cum[i-1][pre_sum][pre_minus_L] + dp[i-1][pre_sum][pre_minus_L]
                res %= 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
                res += cum[i-1][pre_sum][pre_minus_R] - cum[i-1][pre_sum][pre_minus_L] + dp[i-1][pre_sum][pre_minus_L]
                res %= mod

    return res

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

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