結果

問題 No.1036 Make One With GCD 2
ユーザー neterukunneterukun
提出日時 2020-04-24 23:15:15
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,429 bytes
コンパイル時間 247 ms
コンパイル使用メモリ 82,380 KB
実行使用メモリ 190,344 KB
最終ジャッジ日時 2024-04-24 17:08:54
合計ジャッジ時間 37,244 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 AC 359 ms
182,104 KB
testcase_02 AC 485 ms
158,856 KB
testcase_03 AC 126 ms
104,256 KB
testcase_04 AC 170 ms
130,028 KB
testcase_05 AC 38 ms
52,952 KB
testcase_06 AC 39 ms
52,712 KB
testcase_07 AC 178 ms
106,492 KB
testcase_08 AC 153 ms
106,456 KB
testcase_09 AC 908 ms
181,820 KB
testcase_10 AC 919 ms
169,488 KB
testcase_11 AC 853 ms
181,752 KB
testcase_12 AC 858 ms
170,084 KB
testcase_13 AC 1,112 ms
177,552 KB
testcase_14 AC 1,106 ms
187,276 KB
testcase_15 AC 1,151 ms
173,724 KB
testcase_16 AC 1,153 ms
174,176 KB
testcase_17 AC 1,265 ms
177,000 KB
testcase_18 AC 68 ms
71,676 KB
testcase_19 AC 79 ms
76,640 KB
testcase_20 AC 79 ms
76,780 KB
testcase_21 AC 76 ms
76,656 KB
testcase_22 AC 1,160 ms
173,140 KB
testcase_23 AC 884 ms
140,824 KB
testcase_24 AC 1,255 ms
176,228 KB
testcase_25 AC 1,084 ms
161,376 KB
testcase_26 AC 1,173 ms
170,244 KB
testcase_27 AC 40 ms
53,556 KB
testcase_28 AC 39 ms
52,992 KB
testcase_29 AC 39 ms
53,212 KB
testcase_30 AC 41 ms
53,016 KB
testcase_31 AC 45 ms
59,640 KB
testcase_32 AC 45 ms
60,544 KB
testcase_33 AC 41 ms
53,736 KB
testcase_34 AC 43 ms
58,584 KB
testcase_35 AC 40 ms
52,992 KB
testcase_36 AC 39 ms
53,200 KB
testcase_37 AC 40 ms
53,016 KB
testcase_38 AC 674 ms
189,484 KB
testcase_39 TLE -
testcase_40 AC 898 ms
142,052 KB
testcase_41 AC 1,261 ms
189,868 KB
testcase_42 AC 1,309 ms
189,772 KB
testcase_43 AC 1,388 ms
189,904 KB
testcase_44 AC 1,649 ms
189,868 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