結果

問題 No.1036 Make One With GCD 2
ユーザー rlangevinrlangevin
提出日時 2024-01-15 12:12:23
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,400 ms / 2,000 ms
コード長 3,570 bytes
コンパイル時間 378 ms
コンパイル使用メモリ 82,048 KB
実行使用メモリ 200,028 KB
最終ジャッジ日時 2024-09-28 02:15:18
合計ジャッジ時間 25,219 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,249 ms
196,656 KB
testcase_01 AC 395 ms
149,900 KB
testcase_02 AC 600 ms
168,116 KB
testcase_03 AC 130 ms
104,704 KB
testcase_04 AC 178 ms
129,664 KB
testcase_05 AC 43 ms
52,608 KB
testcase_06 AC 43 ms
52,352 KB
testcase_07 AC 282 ms
106,192 KB
testcase_08 AC 238 ms
108,032 KB
testcase_09 AC 525 ms
181,524 KB
testcase_10 AC 474 ms
170,260 KB
testcase_11 AC 531 ms
181,656 KB
testcase_12 AC 505 ms
170,624 KB
testcase_13 AC 737 ms
187,188 KB
testcase_14 AC 747 ms
195,752 KB
testcase_15 AC 694 ms
181,840 KB
testcase_16 AC 684 ms
183,968 KB
testcase_17 AC 718 ms
185,136 KB
testcase_18 AC 76 ms
70,912 KB
testcase_19 AC 80 ms
72,576 KB
testcase_20 AC 83 ms
74,880 KB
testcase_21 AC 80 ms
74,112 KB
testcase_22 AC 674 ms
181,596 KB
testcase_23 AC 506 ms
146,816 KB
testcase_24 AC 697 ms
184,884 KB
testcase_25 AC 655 ms
170,128 KB
testcase_26 AC 682 ms
180,656 KB
testcase_27 AC 42 ms
52,864 KB
testcase_28 AC 42 ms
52,608 KB
testcase_29 AC 42 ms
52,608 KB
testcase_30 AC 44 ms
52,736 KB
testcase_31 AC 47 ms
53,248 KB
testcase_32 AC 49 ms
58,240 KB
testcase_33 AC 44 ms
52,736 KB
testcase_34 AC 46 ms
53,760 KB
testcase_35 AC 42 ms
52,352 KB
testcase_36 AC 43 ms
52,352 KB
testcase_37 AC 42 ms
52,608 KB
testcase_38 AC 1,400 ms
200,016 KB
testcase_39 AC 1,029 ms
199,760 KB
testcase_40 AC 525 ms
146,688 KB
testcase_41 AC 977 ms
200,024 KB
testcase_42 AC 922 ms
200,028 KB
testcase_43 AC 1,127 ms
200,020 KB
testcase_44 AC 1,123 ms
199,888 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegmentTree:
    def __init__(self,
                n,
                identity_e,
                combine_f,
                ):
        self._n = n
        self._size = 1
        while self._size < self._n:
            self._size <<= 1
        self._identity_e = identity_e
        self._combine_f = combine_f
        self._node = [self._identity_e] * (2 * self._size)

    def build(self, array):
        assert len(array) == self._n
        for index, value in enumerate(array, start=self._size):
            self._node[index] = value
        for index in range(self._size - 1, 0, -1):
            self._node[index] = self._combine_f(
                self._node[index << 1 | 0],
                self._node[index << 1 | 1]
            )

    def update(self, index, value):
        i = self._size + index
        self._node[i] = value
        while i > 1:
            i >>= 1
            self._node[i] = self._combine_f(
                self._node[i << 1 | 0],
                self._node[i << 1 | 1]
            )

    def fold(self, L, R):
        L += self._size
        R += self._size
        value_L = self._identity_e
        value_R = self._identity_e
        while L < R:
            if L & 1:
                value_L = self._combine_f(value_L, self._node[L])
                L += 1
            if R & 1:
                R -= 1
                value_R = self._combine_f(self._node[R], value_R)
            L >>= 1
            R >>= 1
        return self._combine_f(value_L, value_R)

    def get(self, p):
        return self._node[p + self._size]

    def max_right(self, l, f):
        assert 0 <= l <= self._n
        assert f(self._identity_e)
        if l == self._n:
            return self._n
        l += self._size
        sm = self._identity_e
        while True:
            while l % 2 == 0:
                l >>= 1
            if not f(self._combine_f(sm, self._node[l])):
                while l < self._size:
                    l <<= 1
                    if f(self._combine_f(sm, self._node[l])):
                        sm = self._combine_f(sm, self._node[l])
                        l += 1
                return l - self._size
            sm = self._combine_f(sm, self._node[l])
            l += 1
            if l & -l == l:
                break
        return self._n

    def min_left(self, r, f):
        assert 0 <= r <= self._n
        assert f(self._identity_e)
        if r == 0:
            return 0
        r += self._size
        sm = self._identity_e
        while True:
            r -= 1
            while r > 1 and r % 2:
                r >>= 1
            if not f(self._combine_f(self._node[r], sm)):
                while r < self._size:
                    r = 2 * r + 1
                    if f(self._combine_f(self._node[r], sm)):
                        sm = self._combine_f(self._node[r], sm)
                        r -= 1
                return r + 1 - self._size
            sm = self._combine_f(self._node[r], sm)
            if r & -r == r:
                break
        return 0
    
from math import gcd
    
N = int(input())
A = list(map(int, input().split()))
T = SegmentTree(N, 0, gcd)
T.build(A)
ans = N * (N + 1) // 2

def f(x):
    return x != 1

for i in range(N):
    ans -= i+1 - T.min_left(i+1, f)
    # if T.fold(i, N) != 1:
    #     ans -= N - i
    #     continue
    
    # yes = i
    # no = N
    # while no - yes != 1:
    #     mid = (yes + no) // 2
    #     if T.fold(i, mid) != 1:
    #         yes = mid
    #     else:
    #         no = mid
    # ans -= yes - i
    
print(ans)
0