結果

問題 No.1239 Multiplication -2
ユーザー shotoyooshotoyoo
提出日時 2020-09-26 00:16:59
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 423 ms / 2,000 ms
コード長 2,378 bytes
コンパイル時間 432 ms
コンパイル使用メモリ 87,036 KB
実行使用メモリ 106,076 KB
最終ジャッジ日時 2023-09-10 17:19:19
合計ジャッジ時間 9,969 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
71,944 KB
testcase_01 AC 103 ms
71,804 KB
testcase_02 AC 99 ms
71,636 KB
testcase_03 AC 116 ms
77,808 KB
testcase_04 AC 118 ms
77,900 KB
testcase_05 AC 114 ms
77,516 KB
testcase_06 AC 102 ms
72,336 KB
testcase_07 AC 102 ms
72,332 KB
testcase_08 AC 98 ms
71,656 KB
testcase_09 AC 99 ms
71,384 KB
testcase_10 AC 98 ms
71,552 KB
testcase_11 AC 98 ms
71,568 KB
testcase_12 AC 96 ms
71,476 KB
testcase_13 AC 97 ms
71,636 KB
testcase_14 AC 98 ms
71,408 KB
testcase_15 AC 307 ms
85,700 KB
testcase_16 AC 352 ms
92,780 KB
testcase_17 AC 161 ms
105,820 KB
testcase_18 AC 168 ms
105,760 KB
testcase_19 AC 163 ms
105,760 KB
testcase_20 AC 175 ms
105,792 KB
testcase_21 AC 267 ms
105,880 KB
testcase_22 AC 263 ms
105,864 KB
testcase_23 AC 250 ms
97,636 KB
testcase_24 AC 240 ms
97,688 KB
testcase_25 AC 126 ms
92,332 KB
testcase_26 AC 178 ms
95,280 KB
testcase_27 AC 319 ms
86,084 KB
testcase_28 AC 423 ms
103,068 KB
testcase_29 AC 411 ms
103,040 KB
testcase_30 AC 354 ms
88,208 KB
testcase_31 AC 367 ms
95,208 KB
testcase_32 AC 423 ms
106,076 KB
testcase_33 AC 382 ms
97,740 KB
testcase_34 AC 363 ms
95,484 KB
testcase_35 AC 340 ms
87,448 KB
testcase_36 AC 333 ms
85,988 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
ls = []
from collections import deque
l = deque()
flg = False
left = True
first = True
right = False
seen0 = False
for num in a:
    if num==0:
        if l:
            if flg:
                if first and seen0:
                    left = False
                first = False
                ls.append(list(l))
            l = deque()
            flg = False
        seen0 = True
    elif (num==2 or num==-2) and flg:
        ls.append(list(l))
        if seen0 and first:
            left = False
            first = False
        while abs(l[0])!=2:
            l.popleft()
        l.popleft()
        l.append(num)
        flg = True
    elif (num==2 or num==-2):
        l.append(num)
        flg = True
    else:
        l.append(num)
if l and flg:
    if seen0 and first:
        left = False
    ls.append(list(l))
    right = True
M = 998244353
inv2 = pow(2, M-2, M)
invs = [None]*(n+10)
invs[0] = 1
for i in range(1, n+10):
    invs[i] = invs[i-1]*inv2
    invs[i] %= M
def sub(l, left=False, right=False):
    if 2 in l:
        ind = l.index(2)
        flg2 = True
    else:
        ind = l.index(-2)
        flg2 = False
    e0 = inv2 if (ind>0 or not left) else 1
    o0 = 0
    e1 = inv2 if (ind<len(l)-1 or not right) else 1
    o1 = 0
    even = True
    for i in range(ind):
        if l[ind-i-1]==-1:
            even = not even
        tmp = invs[i+2] if (i<ind-1 or (not left)) else invs[i+1]
        if even:
            e0 += tmp
        else:
            o0 += tmp
    even = True
    for i in range(len(l)-ind-1):
        if l[ind+i+1]==-1:
            even = not even
        tmp = invs[i+2] if (i<len(l)-ind-2 or (not right)) else invs[i+1]
        if even:
            e1 += tmp
        else:
            o1 += tmp
    if not flg2:
        ans = e0*e1 + o0*o1
    else:
        ans = e0*o1 + e1*o0
#     print(left, right, e0, e1, o0, o1, ans%M)
    return ans%M
if len(ls)==0:
    ans = 0
elif len(ls)==1:
    ans = sub(ls[0], left=left, right=right)
else:
    ans = 0
    ans += sub(ls[0], left=left)
    ans %= M
    for i in range(1, len(ls)-1):
        ans += sub(ls[i])
        ans %= M
    ans += sub(ls[-1], right=right)
    ans %= M
print(ans%M)
0