結果
問題 | No.1555 Constructed Balancing Sequence |
ユーザー | chineristAC |
提出日時 | 2021-01-04 16:42:04 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 3,308 bytes |
コンパイル時間 | 373 ms |
コンパイル使用メモリ | 82,576 KB |
実行使用メモリ | 149,256 KB |
最終ジャッジ日時 | 2024-06-22 19:26:59 |
合計ジャッジ時間 | 6,876 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
52,992 KB |
testcase_01 | AC | 1,314 ms
149,256 KB |
testcase_02 | AC | 107 ms
76,564 KB |
testcase_03 | AC | 116 ms
76,844 KB |
testcase_04 | AC | 137 ms
77,352 KB |
testcase_05 | AC | 108 ms
76,372 KB |
testcase_06 | AC | 89 ms
76,640 KB |
testcase_07 | AC | 69 ms
70,656 KB |
testcase_08 | AC | 84 ms
76,800 KB |
testcase_09 | AC | 87 ms
76,716 KB |
testcase_10 | AC | 63 ms
68,608 KB |
testcase_11 | AC | 98 ms
77,540 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 | 52 ms
61,824 KB |
testcase_39 | AC | 40 ms
53,376 KB |
testcase_40 | AC | 46 ms
60,800 KB |
testcase_41 | RE | - |
testcase_42 | RE | - |
testcase_43 | RE | - |
ソースコード
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(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)) 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 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(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] += 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(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] += 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))