結果

問題 No.1036 Make One With GCD 2
ユーザー neterukunneterukun
提出日時 2020-04-24 23:51:50
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 1,919 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 269,580 KB
最終ジャッジ日時 2024-04-24 18:26:10
合計ジャッジ時間 24,019 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 552 ms
264,744 KB
testcase_01 AC 488 ms
189,276 KB
testcase_02 AC 518 ms
233,112 KB
testcase_03 AC 177 ms
125,568 KB
testcase_04 AC 277 ms
172,672 KB
testcase_05 AC 38 ms
52,352 KB
testcase_06 AC 39 ms
51,840 KB
testcase_07 AC 263 ms
134,160 KB
testcase_08 AC 223 ms
130,236 KB
testcase_09 TLE -
testcase_10 AC 1,862 ms
232,628 KB
testcase_11 TLE -
testcase_12 AC 1,809 ms
233,152 KB
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 AC 74 ms
70,656 KB
testcase_19 AC 80 ms
72,448 KB
testcase_20 AC 82 ms
73,216 KB
testcase_21 AC 80 ms
72,960 KB
testcase_22 TLE -
testcase_23 AC 1,643 ms
189,824 KB
testcase_24 TLE -
testcase_25 AC 1,953 ms
227,672 KB
testcase_26 TLE -
testcase_27 AC 42 ms
52,352 KB
testcase_28 AC 41 ms
52,352 KB
testcase_29 AC 41 ms
52,096 KB
testcase_30 AC 40 ms
52,352 KB
testcase_31 AC 50 ms
59,648 KB
testcase_32 AC 54 ms
60,672 KB
testcase_33 AC 46 ms
52,864 KB
testcase_34 AC 58 ms
60,416 KB
testcase_35 AC 46 ms
52,608 KB
testcase_36 AC 42 ms
52,352 KB
testcase_37 AC 42 ms
52,608 KB
testcase_38 AC 497 ms
268,316 KB
testcase_39 AC 589 ms
268,960 KB
testcase_40 AC 1,506 ms
189,568 KB
testcase_41 AC 1,637 ms
269,476 KB
testcase_42 AC 1,593 ms
269,304 KB
testcase_43 AC 1,395 ms
268,828 KB
testcase_44 AC 1,543 ms
269,216 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)])

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

sp = SparseTable(a)
l, r = 0, 0
ans = 0
while True:
    while True:
        if r == n + 1:
            break
        if l == r:
            r += 1
            continue
        if sp.query(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