結果

問題 No.1334 Multiply or Add
ユーザー chineristACchineristAC
提出日時 2021-01-08 22:38:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 140 ms / 2,000 ms
コード長 1,902 bytes
コンパイル時間 173 ms
コンパイル使用メモリ 82,224 KB
実行使用メモリ 119,208 KB
最終ジャッジ日時 2024-04-28 04:06:20
合計ジャッジ時間 8,309 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
53,572 KB
testcase_01 AC 43 ms
53,424 KB
testcase_02 AC 39 ms
52,560 KB
testcase_03 AC 98 ms
116,300 KB
testcase_04 AC 99 ms
116,608 KB
testcase_05 AC 98 ms
117,132 KB
testcase_06 AC 97 ms
116,372 KB
testcase_07 AC 98 ms
115,852 KB
testcase_08 AC 97 ms
115,500 KB
testcase_09 AC 97 ms
114,936 KB
testcase_10 AC 99 ms
116,924 KB
testcase_11 AC 97 ms
115,872 KB
testcase_12 AC 99 ms
115,688 KB
testcase_13 AC 95 ms
115,840 KB
testcase_14 AC 94 ms
116,108 KB
testcase_15 AC 96 ms
115,916 KB
testcase_16 AC 81 ms
99,056 KB
testcase_17 AC 82 ms
100,956 KB
testcase_18 AC 83 ms
99,276 KB
testcase_19 AC 140 ms
119,208 KB
testcase_20 AC 136 ms
119,116 KB
testcase_21 AC 136 ms
118,840 KB
testcase_22 AC 93 ms
113,092 KB
testcase_23 AC 99 ms
115,804 KB
testcase_24 AC 99 ms
115,820 KB
testcase_25 AC 99 ms
115,972 KB
testcase_26 AC 98 ms
115,712 KB
testcase_27 AC 97 ms
115,400 KB
testcase_28 AC 98 ms
116,044 KB
testcase_29 AC 97 ms
116,572 KB
testcase_30 AC 97 ms
115,980 KB
testcase_31 AC 97 ms
115,524 KB
testcase_32 AC 98 ms
116,280 KB
testcase_33 AC 98 ms
115,924 KB
testcase_34 AC 96 ms
116,160 KB
testcase_35 AC 96 ms
116,168 KB
testcase_36 AC 96 ms
116,832 KB
testcase_37 AC 92 ms
115,708 KB
testcase_38 AC 92 ms
115,176 KB
testcase_39 AC 40 ms
52,860 KB
testcase_40 AC 68 ms
84,788 KB
testcase_41 AC 54 ms
68,316 KB
testcase_42 AC 55 ms
70,752 KB
testcase_43 AC 84 ms
103,116 KB
testcase_44 AC 67 ms
85,696 KB
testcase_45 AC 55 ms
70,552 KB
testcase_46 AC 67 ms
85,216 KB
testcase_47 AC 83 ms
99,824 KB
testcase_48 AC 71 ms
88,392 KB
testcase_49 AC 89 ms
108,936 KB
testcase_50 AC 101 ms
101,656 KB
testcase_51 AC 103 ms
102,160 KB
testcase_52 AC 103 ms
102,012 KB
testcase_53 AC 101 ms
101,704 KB
testcase_54 AC 101 ms
102,016 KB
testcase_55 AC 101 ms
100,112 KB
testcase_56 AC 39 ms
52,868 KB
testcase_57 AC 40 ms
52,660 KB
testcase_58 AC 39 ms
52,480 KB
testcase_59 AC 39 ms
53,284 KB
testcase_60 AC 37 ms
52,632 KB
testcase_61 AC 39 ms
52,400 KB
testcase_62 AC 39 ms
53,220 KB
testcase_63 AC 40 ms
53,268 KB
testcase_64 AC 40 ms
53,680 KB
testcase_65 AC 39 ms
52,888 KB
testcase_66 AC 82 ms
98,904 KB
testcase_67 AC 69 ms
86,980 KB
testcase_68 AC 46 ms
62,380 KB
testcase_69 AC 91 ms
104,288 KB
testcase_70 AC 86 ms
102,980 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

N = int(input())
A = list(map(int,input().split()))
mod = 10**9 + 7

check = 1
for i in range(N):
    check *= A[i]
    if check>2**20:
        check = 2**20
        break

if check==2**20:
    L = -1
    R = N
    for i in range(N):
        if A[i]>1:
            if L==-1:
                L = i
            R = i

    res = 0
    if L!=-1:
        res = L + (N-1-R)

    tmp = 1
    for i in range(L,R+1):
        tmp *= A[i]
        tmp %= mod
    res += tmp
    res %= mod
    print(res)
else:
    comp = []
    tmp = 1
    flag = False
    for i in range(N):
        if A[i]==1:
            if flag:
                comp.append(tmp)
                flag = False
            comp.append(1)
        else:
            if not flag:
                tmp = A[i]
                flag = True
            else:
                tmp *= A[i]
                tmp %= mod
    if flag:
        comp.append(tmp)

    n = len(comp)
    L = -1
    R = N
    for i in range(n):
        if comp[i]!=1:
            if L==-1:
                L = i
            R = i

    if L==-1:
        res = N
        print(res)
    elif L==R:
        res = sum(comp)
        print(res%mod)
    else:
        comp = [comp[i] for i in range(L,R+1)]
        new_comp = []
        for i in range(len(comp)):
            if comp[i]!=1:
                new_comp.append(i)

        res = 0
        m = len(new_comp)
        #print(comp)
        #print(new_comp)
        for i in range(2**(m-1)):
            tmp = L + (n-1-R)
            tmp_prod = comp[new_comp[0]]
            for j in range(m-1):
                if i>>j & 1:
                    tmp_prod *= comp[new_comp[j+1]]
                else:
                    tmp += tmp_prod + new_comp[j+1] - new_comp[j] - 1
                    tmp_prod = comp[new_comp[j+1]]
            tmp += tmp_prod
            #print(tmp,L,R,n,tmp_prod)
            res = max(res,tmp)
        print(res)
0