結果

問題 No.1036 Make One With GCD 2
ユーザー vwxyzvwxyz
提出日時 2024-08-14 20:44:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 849 ms / 2,000 ms
コード長 5,831 bytes
コンパイル時間 474 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 231,016 KB
最終ジャッジ日時 2024-08-14 20:45:04
合計ジャッジ時間 22,092 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 641 ms
227,312 KB
testcase_01 AC 359 ms
189,832 KB
testcase_02 AC 479 ms
190,212 KB
testcase_03 AC 128 ms
118,016 KB
testcase_04 AC 177 ms
146,052 KB
testcase_05 AC 48 ms
55,168 KB
testcase_06 AC 42 ms
54,912 KB
testcase_07 AC 213 ms
125,964 KB
testcase_08 AC 194 ms
114,516 KB
testcase_09 AC 486 ms
188,032 KB
testcase_10 AC 477 ms
198,840 KB
testcase_11 AC 491 ms
188,764 KB
testcase_12 AC 488 ms
199,068 KB
testcase_13 AC 631 ms
217,956 KB
testcase_14 AC 658 ms
227,208 KB
testcase_15 AC 616 ms
214,036 KB
testcase_16 AC 631 ms
214,828 KB
testcase_17 AC 611 ms
217,260 KB
testcase_18 AC 65 ms
71,240 KB
testcase_19 AC 81 ms
76,928 KB
testcase_20 AC 81 ms
76,928 KB
testcase_21 AC 83 ms
76,928 KB
testcase_22 AC 618 ms
212,352 KB
testcase_23 AC 470 ms
169,684 KB
testcase_24 AC 663 ms
215,248 KB
testcase_25 AC 572 ms
200,716 KB
testcase_26 AC 620 ms
211,468 KB
testcase_27 AC 41 ms
54,784 KB
testcase_28 AC 42 ms
54,656 KB
testcase_29 AC 42 ms
54,656 KB
testcase_30 AC 43 ms
55,040 KB
testcase_31 AC 44 ms
56,064 KB
testcase_32 AC 45 ms
56,320 KB
testcase_33 AC 42 ms
55,040 KB
testcase_34 AC 45 ms
55,424 KB
testcase_35 AC 44 ms
55,168 KB
testcase_36 AC 43 ms
54,784 KB
testcase_37 AC 44 ms
54,912 KB
testcase_38 AC 849 ms
231,016 KB
testcase_39 AC 537 ms
231,012 KB
testcase_40 AC 491 ms
169,692 KB
testcase_41 AC 698 ms
230,884 KB
testcase_42 AC 700 ms
230,628 KB
testcase_43 AC 731 ms
231,000 KB
testcase_44 AC 765 ms
230,756 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from math import gcd

class Segment_Tree:
    def __init__(self,N,f,e,lst=None,dynamic=False,bisect_search=True):
        self.f=f
        self.e=e
        self.N=N
        self.bisect_search=bisect_search
        if self.bisect_search:
            self.le=1
            while self.le<self.N:
                self.le*=2
        else:
            self.le=self.N
        if dynamic:
            self.segment_tree=defaultdict(lambda:self.e)
        else:
            if lst==None:
                self.segment_tree=[self.e]*2*self.le
            else:
                assert len(lst)<=self.le
                self.segment_tree=[self.e]*self.le+[x for x in lst]+[self.e]*(self.le-len(lst))
                for i in range(self.le-1,0,-1):
                    self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def __getitem__(self,i):
        if type(i)==int:
            if -self.le<=i<0:
                return self.segment_tree[i+self.le*2]
            elif 0<=i<self.le:
                return self.segment_tree[i+self.le]
            else:
                raise IndexError("list index out of range")
        else:
            a,b,c=i.start,i.stop,i.step
            if a==None:
                a=self.le
            else:
                a+=self.le
            if b==None:
                b=self.le*2
            else:
                b+=self.le
            return self.segment_tree[slice(a,b,c)]

    def __setitem__(self,i,x):
        if -self.le<=i<0:
            i+=self.le*2
        elif 0<=i<self.le:
            i+=self.le
        else:
            raise IndexError("list index out of range")
        self.segment_tree[i]=x
        while i>1:
            i>>= 1
            self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def Build(self,lst):
        for i,x in enumerate(lst,self.le):
            self.segment_tree[i]=x
        for i in range(self.le-1,0,-1):
            self.segment_tree[i]=self.f(self.segment_tree[i<<1],self.segment_tree[i<<1|1])

    def Fold(self,L=None,R=None):
        if L==None:
            L=self.le
        else:
            assert 0<=L<=self.le
            L+=self.le
        if R==None:
            R=self.le*2
        else:
            assert 0<=R<=self.le
            R+=self.le
        vL=self.e
        vR=self.e
        while L<R:
            if L&1:
                vL=self.f(vL,self.segment_tree[L])
                L+=1
            if R&1:
                R-=1
                vR=self.f(self.segment_tree[R],vR)
            L>>=1
            R>>=1
        return self.f(vL,vR)

    def Fold_Index(self,L=None,R=None):
        if L==None:
            L=self.le
        else:
            assert 0<=L<=self.le
            L+=self.le
        if R==None:
            R=self.le*2
        else:
            assert 0<=R<=self.le
            R+=self.le
        if L==R:
            return None
        x=self.Fold(L-self.le,R-self.le)
        while L<R:
            if L&1:
                if self.segment_tree[L]==x:
                    i=L
                    break
                L+=1
            if R&1:
                R-=1
                if self.segment_tree[R]==x:
                    i=R
                    break
            L>>=1
            R>>=1
        while i<self.le:
            if self.segment_tree[i]==self.segment_tree[i<<1]:
                i<<=1
            else:
                i<<=1
                i|=1
        i-=self.le
        return i

    def Bisect_Right(self,L=None,f=None):
        assert self.bisect_search
        if L==self.le:
            return self.le
        if L==None:
            L=0
        assert 0<=L<=self.le
        L+=self.le
        vl=self.e
        vr=self.e
        l,r=L,self.le*2
        while l<r:
            if l&1:
                vl=self.f(vl,self.segment_tree[l])
                l+=1
            if r&1:
                r-=1
                vr=self.f(self.segment_tree[r],vr)
            l>>=1
            r>>=1
        if f(self.f(vl,vr)):
            return self.N
        v=self.e
        while True:
            while L%2==0:
                L>>=1
            vv=self.f(v,self.segment_tree[L])
            if f(vv):
                v=vv
                L+=1
            else:
                while L<self.le:
                    L<<=1
                    vv=self.f(v,self.segment_tree[L])
                    if f(vv):
                        v=vv
                        L+=1
                return L-self.le

    def Bisect_Left(self,R=None,f=None):
        assert self.bisect_search
        if R==0:
            return 0
        if R==None:
            R=self.le
        assert 0<=R<=self.le
        R+=self.le
        vl=self.e
        vr=self.e
        l,r=self.le,R
        while l<r:
            if l&1:
                vl=self.f(vl,self.segment_tree[l])
                l+=1
            if r&1:
                r-=1
                vr=self.f(self.segment_tree[r],vr)
            l>>=1
            r>>=1
        if f(self.f(vl,vr)):
            return 0
        v=self.e
        while True:
            R-=1
            while R>1 and R%2:
                R>>=1
            vv=self.f(self.segment_tree[R],v)
            if f(vv):
                v=vv
            else:
                while R<self.le:
                    R=2*R+1
                    vv=self.f(self.segment_tree[R],v)
                    if f(vv):
                        v=vv
                        R-=1
                return R+1-self.le

    def __str__(self):
        return "["+", ".join(map(str,[self.segment_tree[i] for i in range(self.le,self.le+self.N)]))+"]"

N=int(input())
A=list(map(int,input().split()))
ST=Segment_Tree(N,gcd,0,A)
ans=0
r=0
for l in range(N):
    while r<N and ST.Fold(l,r+1)!=1:
        r+=1
    ans+=N-r
print(ans)
0