結果

問題 No.1036 Make One With GCD 2
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2022-03-27 23:35:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 649 ms / 2,000 ms
コード長 1,190 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 270,928 KB
最終ジャッジ日時 2024-04-24 09:18:55
合計ジャッジ時間 18,079 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 296 ms
190,296 KB
testcase_01 AC 332 ms
174,112 KB
testcase_02 AC 364 ms
235,636 KB
testcase_03 AC 100 ms
100,864 KB
testcase_04 AC 131 ms
121,472 KB
testcase_05 AC 37 ms
52,608 KB
testcase_06 AC 37 ms
52,224 KB
testcase_07 AC 196 ms
102,912 KB
testcase_08 AC 171 ms
103,936 KB
testcase_09 AC 470 ms
173,092 KB
testcase_10 AC 421 ms
161,512 KB
testcase_11 AC 464 ms
173,992 KB
testcase_12 AC 424 ms
162,420 KB
testcase_13 AC 625 ms
177,624 KB
testcase_14 AC 649 ms
187,924 KB
testcase_15 AC 611 ms
174,256 KB
testcase_16 AC 622 ms
174,652 KB
testcase_17 AC 605 ms
177,568 KB
testcase_18 AC 63 ms
67,840 KB
testcase_19 AC 68 ms
70,144 KB
testcase_20 AC 73 ms
72,320 KB
testcase_21 AC 70 ms
72,064 KB
testcase_22 AC 580 ms
173,764 KB
testcase_23 AC 442 ms
138,240 KB
testcase_24 AC 606 ms
176,776 KB
testcase_25 AC 554 ms
161,392 KB
testcase_26 AC 605 ms
170,496 KB
testcase_27 AC 35 ms
52,608 KB
testcase_28 AC 36 ms
52,224 KB
testcase_29 AC 35 ms
52,352 KB
testcase_30 AC 35 ms
52,096 KB
testcase_31 AC 42 ms
52,864 KB
testcase_32 AC 44 ms
53,376 KB
testcase_33 AC 37 ms
52,480 KB
testcase_34 AC 38 ms
53,760 KB
testcase_35 AC 35 ms
52,352 KB
testcase_36 AC 36 ms
51,840 KB
testcase_37 AC 37 ms
52,352 KB
testcase_38 AC 325 ms
270,928 KB
testcase_39 AC 302 ms
190,668 KB
testcase_40 AC 467 ms
138,240 KB
testcase_41 AC 338 ms
190,544 KB
testcase_42 AC 341 ms
190,808 KB
testcase_43 AC 338 ms
209,360 KB
testcase_44 AC 335 ms
193,748 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from math import gcd

class SWAG:

    def __init__(self,f):
        self.st1 = []
        self.st2 = []
        self.rst1 = []
        self.rst2 = []
        self.f = f


    def calc_all(self):
        calc = 0
        if self.st1:
            calc = self.f(calc,self.rst1[-1])

        if self.st2:
            calc = self.f(calc,self.rst2[-1])
        return calc


    def pop_front(self):
        if self.st1 == []:
            self.st1 = self.st2[::-1]
            self.rst1 = self.st1[:]
            for i in range(1,len(self.st1)):
                self.rst1[i] = self.f(self.rst1[i-1],self.st1[i])
            self.st2 = []
            self.rst2 = []

        self.st1.pop()
        self.rst1.pop()

    def push_back(self,val):
        if self.st2 == []:
            self.rst2.append(val)
        else:
            self.rst2.append(self.f(self.rst2[-1],val))
        self.st2.append(val)


    
n = int(input())
A = list(map(int,input().split()))
swag = SWAG(gcd)
ans = 0
now = 0
for i,a in enumerate(A):
    while now < n and swag.calc_all() != 1:
        swag.push_back(A[now])
        now += 1
    if swag.calc_all() == 1:
        ans += n-now+1
    swag.pop_front()
print(ans)
0