結果
問題 | No.1555 Constructed Balancing Sequence |
ユーザー | chineristAC |
提出日時 | 2021-01-04 03:13:35 |
言語 | PyPy3 (7.3.15) |
結果 |
MLE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 4,683 bytes |
コンパイル時間 | 213 ms |
コンパイル使用メモリ | 82,140 KB |
実行使用メモリ | 766,920 KB |
最終ジャッジ日時 | 2024-06-22 19:26:41 |
合計ジャッジ時間 | 4,781 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 35 ms
60,480 KB |
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 | -- | - |
ソースコード
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(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)] 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))