結果

問題 No.1036 Make One With GCD 2
ユーザー neterukunneterukun
提出日時 2020-04-25 00:06:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,879 ms / 2,000 ms
コード長 1,848 bytes
コンパイル時間 179 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 260,340 KB
最終ジャッジ日時 2024-04-24 18:28:59
合計ジャッジ時間 13,798 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 535 ms
256,472 KB
testcase_01 AC 472 ms
189,772 KB
testcase_02 AC 491 ms
223,556 KB
testcase_03 AC 170 ms
125,652 KB
testcase_04 AC 267 ms
172,160 KB
testcase_05 AC 38 ms
52,608 KB
testcase_06 AC 36 ms
51,968 KB
testcase_07 AC 239 ms
133,348 KB
testcase_08 AC 211 ms
128,456 KB
testcase_09 AC 1,764 ms
249,588 KB
testcase_10 AC 1,563 ms
232,632 KB
testcase_11 AC 1,734 ms
251,544 KB
testcase_12 AC 1,525 ms
233,680 KB
testcase_13 AC 1,841 ms
243,296 KB
testcase_14 AC 1,879 ms
254,008 KB
testcase_15 AC 1,648 ms
236,824 KB
testcase_16 AC 1,662 ms
237,932 KB
testcase_17 AC 1,723 ms
242,480 KB
testcase_18 AC 65 ms
69,760 KB
testcase_19 AC 66 ms
70,400 KB
testcase_20 AC 65 ms
70,784 KB
testcase_21 AC 67 ms
70,528 KB
testcase_22 AC 1,639 ms
235,028 KB
testcase_23 AC 1,239 ms
183,936 KB
testcase_24 AC 1,721 ms
240,788 KB
testcase_25 AC 1,586 ms
219,496 KB
testcase_26 AC 1,650 ms
230,608 KB
testcase_27 AC 37 ms
52,480 KB
testcase_28 AC 35 ms
52,864 KB
testcase_29 AC 34 ms
52,096 KB
testcase_30 AC 35 ms
52,352 KB
testcase_31 AC 46 ms
60,544 KB
testcase_32 AC 46 ms
60,672 KB
testcase_33 AC 36 ms
52,864 KB
testcase_34 AC 44 ms
60,544 KB
testcase_35 AC 36 ms
52,096 KB
testcase_36 AC 37 ms
52,096 KB
testcase_37 AC 35 ms
52,480 KB
testcase_38 AC 461 ms
259,564 KB
testcase_39 AC 548 ms
259,692 KB
testcase_40 AC 1,252 ms
184,320 KB
testcase_41 AC 1,483 ms
259,944 KB
testcase_42 AC 1,441 ms
259,836 KB
testcase_43 AC 1,249 ms
260,340 KB
testcase_44 AC 1,395 ms
260,332 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 SparseTable:
    """区間取得クエリをO(1)で答えるデータ構造をO(NlogN)で構築する
    query(l, r): 区間[l, r)に対するクエリに答える
    """
    def __init__(self, array):
        n = len(array)
        self.row_size = n.bit_length()

        # log_tableを構築する
        # log_table = [0, 0, 1, 1, 2, 2, 2, 2, ...]
        self.log_table = [0] * (n + 1)
        for i in range(2, n + 1):
            self.log_table[i] = self.log_table[i//2] + 1

        # sparse_tableを構築する
        self.sparse_table = [[0] * n for _ in range(self.row_size)]
        for i in range(n):
            self.sparse_table[0][i] = array[i]
        for row in range(1, self.row_size):
            for i in range(n - (1 << row) + 1):
                self.sparse_table[row][i] = self._merge(self.sparse_table[row - 1][i], \
                                            self.sparse_table[row - 1][i + (1 << row - 1)])

    def _merge(self, num1, num2):
        """冪等律と結合律を満たす演算を行う"""
        return gcd(num1, num2)

    def query(self, l, r):
        """区間[l, r)に対するクエリに答える"""
        row = self.log_table[r - l]
        return self._merge(self.sparse_table[row][l], self.sparse_table[row][r - (1 << row)])

import sys
input = sys.stdin.readline
      
n = int(input())
a = list(map(int, input().split()))

sp = SparseTable(a)
r = 0
ans = 0
for l in range(0, n):
    r = max(l + 1, r)
    while True:
        if r == n + 1:
            break
        if sp.query(l, r) != 1:
            r += 1
        else:
            break
    ans += n - r + 1
    
print(ans)
0