結果

問題 No.1036 Make One With GCD 2
ユーザー convexineqconvexineq
提出日時 2020-04-24 22:10:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 877 ms / 2,000 ms
コード長 1,752 bytes
コンパイル時間 467 ms
コンパイル使用メモリ 86,392 KB
実行使用メモリ 172,224 KB
最終ジャッジ日時 2023-10-14 19:20:51
合計ジャッジ時間 18,056 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 306 ms
145,912 KB
testcase_01 AC 289 ms
148,184 KB
testcase_02 AC 316 ms
172,224 KB
testcase_03 AC 114 ms
95,252 KB
testcase_04 AC 134 ms
110,396 KB
testcase_05 AC 83 ms
71,232 KB
testcase_06 AC 82 ms
71,552 KB
testcase_07 AC 447 ms
110,560 KB
testcase_08 AC 382 ms
103,684 KB
testcase_09 AC 479 ms
147,808 KB
testcase_10 AC 447 ms
142,544 KB
testcase_11 AC 467 ms
148,136 KB
testcase_12 AC 440 ms
142,648 KB
testcase_13 AC 877 ms
145,228 KB
testcase_14 AC 633 ms
150,008 KB
testcase_15 AC 607 ms
143,540 KB
testcase_16 AC 610 ms
143,480 KB
testcase_17 AC 624 ms
144,312 KB
testcase_18 AC 102 ms
77,776 KB
testcase_19 AC 108 ms
77,760 KB
testcase_20 AC 109 ms
77,904 KB
testcase_21 AC 111 ms
78,004 KB
testcase_22 AC 613 ms
146,344 KB
testcase_23 AC 478 ms
131,896 KB
testcase_24 AC 630 ms
144,412 KB
testcase_25 AC 590 ms
141,764 KB
testcase_26 AC 598 ms
145,984 KB
testcase_27 AC 83 ms
71,476 KB
testcase_28 AC 80 ms
71,248 KB
testcase_29 AC 85 ms
71,176 KB
testcase_30 AC 84 ms
71,524 KB
testcase_31 AC 84 ms
71,416 KB
testcase_32 AC 85 ms
72,132 KB
testcase_33 AC 85 ms
71,292 KB
testcase_34 AC 85 ms
71,592 KB
testcase_35 AC 82 ms
71,164 KB
testcase_36 AC 86 ms
71,304 KB
testcase_37 AC 82 ms
71,384 KB
testcase_38 AC 297 ms
163,108 KB
testcase_39 AC 306 ms
146,520 KB
testcase_40 AC 484 ms
131,976 KB
testcase_41 AC 360 ms
146,648 KB
testcase_42 AC 351 ms
146,388 KB
testcase_43 AC 357 ms
151,536 KB
testcase_44 AC 348 ms
146,552 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