結果

問題 No.1036 Make One With GCD 2
ユーザー convexineqconvexineq
提出日時 2020-04-24 22:09:38
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,937 ms / 2,000 ms
コード長 1,752 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 73,452 KB
最終ジャッジ日時 2024-04-24 17:44:32
合計ジャッジ時間 42,013 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,465 ms
65,812 KB
testcase_01 AC 1,352 ms
42,184 KB
testcase_02 AC 1,094 ms
73,452 KB
testcase_03 AC 223 ms
14,304 KB
testcase_04 AC 363 ms
16,424 KB
testcase_05 AC 30 ms
10,880 KB
testcase_06 AC 32 ms
11,008 KB
testcase_07 AC 441 ms
24,240 KB
testcase_08 AC 367 ms
21,688 KB
testcase_09 AC 1,421 ms
64,368 KB
testcase_10 AC 1,341 ms
60,496 KB
testcase_11 AC 1,451 ms
65,576 KB
testcase_12 AC 1,345 ms
61,120 KB
testcase_13 AC 1,599 ms
63,100 KB
testcase_14 AC 1,630 ms
63,724 KB
testcase_15 AC 1,513 ms
60,416 KB
testcase_16 AC 1,520 ms
60,948 KB
testcase_17 AC 1,582 ms
62,308 KB
testcase_18 AC 34 ms
10,880 KB
testcase_19 AC 35 ms
11,008 KB
testcase_20 AC 40 ms
11,136 KB
testcase_21 AC 39 ms
11,136 KB
testcase_22 AC 1,480 ms
59,756 KB
testcase_23 AC 1,113 ms
46,808 KB
testcase_24 AC 1,536 ms
61,944 KB
testcase_25 AC 1,419 ms
57,368 KB
testcase_26 AC 1,477 ms
58,992 KB
testcase_27 AC 30 ms
11,008 KB
testcase_28 AC 32 ms
10,880 KB
testcase_29 AC 31 ms
10,880 KB
testcase_30 AC 32 ms
11,008 KB
testcase_31 AC 32 ms
10,752 KB
testcase_32 AC 32 ms
10,880 KB
testcase_33 AC 30 ms
10,880 KB
testcase_34 AC 32 ms
10,880 KB
testcase_35 AC 31 ms
10,880 KB
testcase_36 AC 30 ms
10,880 KB
testcase_37 AC 31 ms
11,008 KB
testcase_38 AC 1,082 ms
65,772 KB
testcase_39 AC 1,937 ms
65,648 KB
testcase_40 AC 1,099 ms
46,684 KB
testcase_41 AC 1,692 ms
65,768 KB
testcase_42 AC 1,682 ms
65,768 KB
testcase_43 AC 1,584 ms
65,772 KB
testcase_44 AC 1,654 ms
65,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""
sliding window aggrigation
モノイドのqueueを管理する
fold_all: すべてをたたみこむ
popleft
append
"""
from itertools import accumulate
from collections import deque
class SWAG:
    def __init__(self, operator_M, e_M):
        self.op_M = operator_M
        self.op_rev = lambda x,y: self.op_M(y,x)
        self.e_M = e_M
        self.q = deque([])
        self.accL = []
        self.accR = e_M
        self.L = self.R = 0

    def build(self,lst):
        self.q = deque(lst)
        self.L = len(lst)
        self.accL = list(accumulate(reversed(lst),self.op_rev))

    def __len__(self):
        return self.L + self.R

    def fold_all(self):
        if self.L: return self.op_M(self.accL[-1],self.accR)
        else: return self.accR

    def append(self,x):
        self.q.append(x)
        self.accR = self.op_M(self.accR,x)
        self.R += 1
    
    def popleft(self):
        if self.L:
            self.accL.pop()
            self.L -= 1
            return self.q.popleft()
        elif self.R:
            v = self.q.popleft()
            self.L,self.R = self.R-1,0
            self.accL = list(accumulate(reversed(self.q),self.op_rev))
            self.accR = self.e_M
            return v
        else:
            assert 0

    def __repr__(self):
        return "{}\naccL:{}, accR:{}".format(self.q,self.accL,self.accR)




# coding: utf-8
# Your code here!
import sys
readline = sys.stdin.readline
read = sys.stdin.read


n,*a = [int(i) for i in read().split()]

from math import gcd
q = SWAG(gcd,0)
ans = r = 0
for l,al in enumerate(a):
    if len(q): q.popleft()
    else: r = l
    while r<n and gcd(q.fold_all(),a[r]) != 1:
        q.append(a[r])
        r += 1
    ans += n-r

print(ans)        










0