結果

問題 No.1036 Make One With GCD 2
ユーザー neterukunneterukun
提出日時 2020-04-24 23:15:15
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,429 bytes
コンパイル時間 468 ms
コンパイル使用メモリ 86,584 KB
実行使用メモリ 193,936 KB
最終ジャッジ日時 2023-08-06 22:44:28
合計ジャッジ時間 37,937 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 374 ms
157,212 KB
testcase_02 AC 495 ms
160,332 KB
testcase_03 AC 145 ms
105,324 KB
testcase_04 AC 196 ms
120,560 KB
testcase_05 AC 71 ms
71,304 KB
testcase_06 AC 71 ms
71,208 KB
testcase_07 AC 199 ms
111,112 KB
testcase_08 AC 182 ms
102,176 KB
testcase_09 AC 955 ms
182,448 KB
testcase_10 AC 829 ms
173,280 KB
testcase_11 AC 994 ms
156,128 KB
testcase_12 AC 877 ms
173,624 KB
testcase_13 AC 1,136 ms
183,396 KB
testcase_14 AC 1,151 ms
190,860 KB
testcase_15 AC 1,145 ms
178,172 KB
testcase_16 AC 1,152 ms
180,712 KB
testcase_17 AC 1,293 ms
183,172 KB
testcase_18 AC 102 ms
77,524 KB
testcase_19 AC 104 ms
77,644 KB
testcase_20 AC 106 ms
77,468 KB
testcase_21 AC 107 ms
77,528 KB
testcase_22 AC 1,150 ms
176,464 KB
testcase_23 AC 921 ms
140,928 KB
testcase_24 AC 1,201 ms
182,164 KB
testcase_25 AC 1,075 ms
166,600 KB
testcase_26 AC 1,185 ms
175,128 KB
testcase_27 AC 71 ms
70,840 KB
testcase_28 AC 71 ms
71,120 KB
testcase_29 AC 72 ms
71,220 KB
testcase_30 AC 72 ms
70,836 KB
testcase_31 AC 76 ms
75,316 KB
testcase_32 AC 76 ms
75,132 KB
testcase_33 AC 74 ms
71,332 KB
testcase_34 AC 75 ms
74,772 KB
testcase_35 AC 70 ms
71,060 KB
testcase_36 AC 72 ms
70,852 KB
testcase_37 AC 73 ms
71,328 KB
testcase_38 AC 706 ms
192,416 KB
testcase_39 TLE -
testcase_40 AC 933 ms
139,896 KB
testcase_41 AC 1,322 ms
193,396 KB
testcase_42 AC 1,353 ms
193,220 KB
testcase_43 AC 1,412 ms
193,068 KB
testcase_44 AC 1,697 ms
193,936 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def gcd(a: int, b: int) -> int:
    """a, bの最大公約数(greatest common divisor: GCD)を求める
    計算量: O(log(min(a, b)))
    """
    if b == 0:
        return a
    return gcd(b, a % b)


class SegmentTree():
    """一点更新、区間取得クエリをそれぞれO(logN)で答えるデータ構造を構築する。
    build(array)   := arrayを初期値とするセグメント木を構築する O(N)。
    update(i, val) := i番目の要素をvalに変更する。
    get_val(l, r)  := 区間[l, r)に対する二項演算の畳み込みの結果を返す。
    """
    def __init__(self, n, op, e):
        """要素数、二項演算、単位元を引数として渡す
        例) 区間最小値 SegmentTree(n, min, 10 ** 18)
            区間和     SegmentTree(n, lambda a, b : a + b, 0)
        """
        self.n = n
        self.op = op
        self.e = e
        self.size = 2 ** ((n - 1).bit_length())
        self.node = [self.e] * (2 * self.size)

    def build(self, array):
        """arrayを初期値とするセグメント木を構築する"""
        for i in range(self.n):
            self.node[self.size + i] = array[i]
        for i in range(self.size - 1, 0, -1):
            self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1])

    def update(self, i, val):
        """i番目の要素をvalに変更する"""
        i += self.size
        self.node[i] = val
        while i > 1:
            i >>= 1
            self.node[i] = self.op(self.node[i << 1], self.node[(i << 1) + 1])

    def get_val(self, l, r):
        """[l, r)の畳み込みの結果を返す"""
        l, r = l + self.size, r + self.size
        res_l, res_r = self.e, self.e
        while l < r:
            if l & 1:
                res_l = self.op(res_l, self.node[l])
                l += 1
            if r & 1:
                r -= 1
                res_r = self.op(self.node[r], res_r)
            l, r = l >> 1, r >> 1
        return self.op(res_l, res_r)


import sys
input = sys.stdin.readline

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

st = SegmentTree(n, gcd, 0)
st.build(a)

l, r = 0, 0
ans = 0
while True:
    while True:
        if r == n + 1:
            break
        if st.get_val(l, r) == 1:
            ans += n - r + 1
            break
        else:
            r += 1
    l += 1
    if l > r:
        r = l
    if l == n + 1:
        break

print(ans)
0