結果

問題 No.1036 Make One With GCD 2
ユーザー H3PO4H3PO4
提出日時 2021-10-02 12:38:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 730 ms / 2,000 ms
コード長 2,105 bytes
コンパイル時間 2,129 ms
コンパイル使用メモリ 86,648 KB
実行使用メモリ 179,148 KB
最終ジャッジ日時 2023-09-27 12:09:17
合計ジャッジ時間 24,589 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 730 ms
161,508 KB
testcase_01 AC 318 ms
179,148 KB
testcase_02 AC 433 ms
157,728 KB
testcase_03 AC 128 ms
109,500 KB
testcase_04 AC 172 ms
124,436 KB
testcase_05 AC 60 ms
71,536 KB
testcase_06 AC 58 ms
71,320 KB
testcase_07 AC 196 ms
109,504 KB
testcase_08 AC 174 ms
110,508 KB
testcase_09 AC 469 ms
178,120 KB
testcase_10 AC 460 ms
165,448 KB
testcase_11 AC 461 ms
178,636 KB
testcase_12 AC 469 ms
165,644 KB
testcase_13 AC 594 ms
153,888 KB
testcase_14 AC 603 ms
158,248 KB
testcase_15 AC 540 ms
160,608 KB
testcase_16 AC 569 ms
157,824 KB
testcase_17 AC 580 ms
156,172 KB
testcase_18 AC 76 ms
76,756 KB
testcase_19 AC 84 ms
76,612 KB
testcase_20 AC 86 ms
77,676 KB
testcase_21 AC 85 ms
77,808 KB
testcase_22 AC 558 ms
160,788 KB
testcase_23 AC 445 ms
148,720 KB
testcase_24 AC 559 ms
156,272 KB
testcase_25 AC 548 ms
171,428 KB
testcase_26 AC 557 ms
164,208 KB
testcase_27 AC 64 ms
71,440 KB
testcase_28 AC 63 ms
71,536 KB
testcase_29 AC 62 ms
71,544 KB
testcase_30 AC 62 ms
71,540 KB
testcase_31 AC 63 ms
71,424 KB
testcase_32 AC 64 ms
71,472 KB
testcase_33 AC 61 ms
71,344 KB
testcase_34 AC 63 ms
71,348 KB
testcase_35 AC 61 ms
71,216 KB
testcase_36 AC 62 ms
71,264 KB
testcase_37 AC 60 ms
71,268 KB
testcase_38 AC 647 ms
164,200 KB
testcase_39 AC 549 ms
164,576 KB
testcase_40 AC 462 ms
148,880 KB
testcase_41 AC 633 ms
164,948 KB
testcase_42 AC 611 ms
164,512 KB
testcase_43 AC 643 ms
164,644 KB
testcase_44 AC 668 ms
164,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from math import gcd

input = sys.stdin.buffer.readline


class SegTree:
    """
    https://qiita.com/takayg1/items/c811bd07c21923d7ec69 から拝借しています。
    init(init_val, ide_ele): 配列init_valで初期化 O(N)
    update(k, x): k番目の値をxに更新 O(logN)
    query(l, r): 区間[l, r)をsegfuncしたものを返す O(logN)
    """

    def __init__(self, init_val, segfunc, ide_ele):
        """
        init_val: 配列の初期値
        segfunc: 区間にしたい操作
        ide_ele: 単位元
        n: 要素数
        num: n以上の最小の2のべき乗
        tree: セグメント木(1-index)
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新
        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            self.tree[k >> 1] = self.segfunc(self.tree[k], self.tree[k ^ 1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る
        l: index(0-index)
        r: index(0-index)
        """
        res = self.ide_ele

        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                res = self.segfunc(res, self.tree[r - 1])
            l >>= 1
            r >>= 1
        return res


N = int(input())
A = tuple(map(int, input().split()))
seg = SegTree(A, gcd, 0)

l = 0
ans = 0
for r in range(1, N + 1):
    while seg.query(l, r) == 1 and r - l >= 1:
        l += 1
    ans += l
print(ans)
0