結果

問題 No.1036 Make One With GCD 2
ユーザー hedwig100hedwig100
提出日時 2020-04-25 12:11:11
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,029 bytes
コンパイル時間 1,117 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 199,640 KB
最終ジャッジ日時 2024-04-24 19:21:44
合計ジャッジ時間 19,393 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 810 ms
196,640 KB
testcase_01 AC 265 ms
161,400 KB
testcase_02 AC 545 ms
159,348 KB
testcase_03 AC 107 ms
104,064 KB
testcase_04 AC 133 ms
117,428 KB
testcase_05 AC 51 ms
60,672 KB
testcase_06 AC 54 ms
60,416 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 447 ms
158,636 KB
testcase_10 AC 429 ms
161,152 KB
testcase_11 AC 446 ms
159,696 KB
testcase_12 AC 414 ms
161,152 KB
testcase_13 AC 625 ms
186,672 KB
testcase_14 AC 626 ms
196,772 KB
testcase_15 AC 586 ms
182,108 KB
testcase_16 AC 652 ms
184,148 KB
testcase_17 AC 623 ms
185,976 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 AC 592 ms
181,852 KB
testcase_23 AC 456 ms
139,648 KB
testcase_24 AC 637 ms
184,312 KB
testcase_25 AC 553 ms
168,972 KB
testcase_26 AC 586 ms
179,384 KB
testcase_27 AC 50 ms
60,928 KB
testcase_28 AC 51 ms
60,416 KB
testcase_29 AC 50 ms
60,416 KB
testcase_30 AC 52 ms
60,160 KB
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 51 ms
60,416 KB
testcase_36 AC 53 ms
60,160 KB
testcase_37 AC 53 ms
60,672 KB
testcase_38 AC 628 ms
199,256 KB
testcase_39 AC 643 ms
199,380 KB
testcase_40 AC 468 ms
139,776 KB
testcase_41 AC 691 ms
199,516 KB
testcase_42 AC 670 ms
199,640 KB
testcase_43 AC 761 ms
199,636 KB
testcase_44 AC 770 ms
199,512 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

INF = 10 ** 9
MOD = 10 **9 + 7
import sys
sys.setrecursionlimit(100000000)
dy = (-1,0,1,0)
dx = (0,1,0,-1)
from math import gcd
from collections import defaultdict
from copy import deepcopy

class SegmentTree():

    def __init__(self,init_val,func,unit): #init_valは長さnの配列 O(2*n)
        n = len(init_val)
        self.unit = unit #unitは単位元
        self.size = pow(2,n - 1).bit_length() #n以上の最小の2のべき乗
        self.seg = [self.unit] * (2 * self.size) #セグメントツリー本体
        self.f = func #セグ木により異なる関数
        
        for i in range(n):
            self.seg[i + self.size - 1] = init_val[i] 
        
        for i in range(self.size - 2,-1,-1):
            self.seg[i] = self.f(self.seg[2 * i + 1],self.seg[2 * i + 2])
    
    def update(self,k,x): #k番目の要素をxに変更する O(logN)
        k += self.size - 1
        self.seg[k] = x
        while k:
            k = (k - 1)//2
            self.seg[k] = self.f(self.seg[2 * k + 1],self.seg[2 * k + 2])
    
    def query(self,p,q): #[p,q)のクエリに答える 半開区間であることに注意 O(logN)
        if q <= p:
            return self.unit

        p += self.size - 1 
        q += self.size - 2
        res = self.unit

        while q - p > 1:

            if (p&1) == 0:
                res = self.f(res,self.seg[p])

            if (q&1) == 1:
                res = self.f(res,self.seg[q])
                q -= 1

            p //= 2
            q = (q - 1)//2
        
        if p == q:
            res = self.f(res,self.seg[p])
        else:
            res = self.f(self.f(res,self.seg[p]),self.seg[q])
        
        return res

def main():
    n = int(input())
    a = list(map(int,input().split()))

    st = SegmentTree(a,gcd,0)
    r = 0
    ans = 0
    for l in range(n):
        while r < n and st.query(l,r + 1) != 1:
            r += 1
        ans += n - r
        if l + 1 == r:
            r += 1
    print(ans)
if __name__=='__main__':
    main()

0