結果

問題 No.1036 Make One With GCD 2
ユーザー terasa
提出日時 2022-05-28 18:39:39
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,678 bytes
コンパイル時間 214 ms
コンパイル使用メモリ 82,160 KB
実行使用メモリ 230,296 KB
最終ジャッジ日時 2024-09-20 23:27:59
合計ジャッジ時間 24,447 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 22 WA * 19
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import pypyjit
import itertools
import heapq
import math
from collections import deque, defaultdict, Counter
import bisect

input = sys.stdin.readline
sys.setrecursionlimit(10 ** 6)
pypyjit.set_param('max_unroll_recursion=-1')


def index_lt(a, x):
    'return largest index s.t. A[i] < x or -1 if it does not exist'
    return bisect.bisect_left(a, x) - 1


def index_le(a, x):
    'return largest index s.t. A[i] <= x or -1 if it does not exist'
    return bisect.bisect_right(a, x) - 1


def index_gt(a, x):
    'return smallest index s.t. A[i] > x or len(a) if it does not exist'
    return bisect.bisect_right(a, x)


def index_ge(a, x):
    'return smallest index s.t. A[i] >= x or len(a) if it does not exist'
    return bisect.bisect_left(a, x)


class SegTree:
    def __init__(self, N, func, e):
        self.N = N
        self.func = func
        self.X = [e] * (N << 1)
        self.e = e

    def build(self, seq):
        for i in range(self.N):
            self.X[self.N + i] = seq[i]
        for i in range(self.N)[::-1]:
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def add(self, i, x):
        i += self.N
        self.X[i] += x
        while i > 1:
            i >>= 1
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def update(self, i, x):
        i += self.N
        self.X[i] = x
        while i > 1:
            i >>= 1
            self.X[i] = self.func(self.X[i << 1], self.X[i << 1 | 1])

    def query(self, L, R):
        L += self.N
        R += self.N
        vL = self.X[L]
        vR = self.X[R - 1]
        while L < R:
            if L & 1:
                vL = self.func(vL, self.X[L])
                L += 1
            if R & 1:
                R -= 1
                vR = self.func(self.X[R], vR)
            L >>= 1
            R >>= 1
        return self.func(vL, vR)


N = int(input())
A = list(map(int, input().split()))

B = []
tmp = []
cnt1 = 0
for a in A:
    if a == 1:
        cnt1 += 1
        if len(tmp) > 0:
            B.append(tmp)
            tmp = []
    else:
        tmp.append(a)
if len(tmp) > 0:
    B.append(tmp)

ans = (1 + N) * N // 2 - (N - cnt1)
for b in B:
    st = SegTree(len(b), math.gcd, 1)
    st.build(b)

    def solve(b):
        global ans
        l = 0
        r = 1
        while r <= len(b):
            while st.query(l, r) > 1:
                if r == len(b):
                    d = r - l
                    ans -= d * (d - 1) // 2
                    return
                r += 1
            d = r - l - 1
            ans -= d * (d - 1) // 2
            while st.query(l, r) == 1:
                l += 1
    solve(b)
print(ans)
0