結果

問題 No.1036 Make One With GCD 2
ユーザー ikdikd
提出日時 2020-04-25 17:02:33
言語 Nim
(2.0.2)
結果
TLE  
実行時間 -
コード長 1,706 bytes
コンパイル時間 4,256 ms
コンパイル使用メモリ 65,792 KB
実行使用メモリ 45,952 KB
最終ジャッジ日時 2024-04-24 23:19:47
合計ジャッジ時間 12,716 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
testcase_36 -- -
testcase_37 -- -
testcase_38 -- -
testcase_39 -- -
testcase_40 -- -
testcase_41 -- -
testcase_42 -- -
testcase_43 -- -
testcase_44 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import strutils, sequtils, math

type SegmentTree[T] = object
  n: int
  dat: seq[T]
  e: T
  multiply: proc(a, b: T): T

proc initSegmentTree[T](n: int, e: T, f: proc(a, b: T): T): SegmentTree[T] =
  let nn = nextPowerOfTwo(n)
  var dat = newSeqWith(nn * 2 - 1, e)
  return SegmentTree[T](n: nn, dat: dat, e: e, multiply: f)

proc get[T](this: SegmentTree[T], i: int): T =
  return this.dat[i + this.n - 1]

proc update[T](this: var SegmentTree[T], i: int, x: T) =
  var k = i + this.n - 1
  this.dat[k] = x
  while k > 0:
    k = (k - 1) div 2
    this.dat[k] = this.multiply(this.dat[k * 2 + 1], this.dat[k * 2 + 2])

proc query[T](this: SegmentTree[T], ql, qr, i, il, ir: int): T =
  if ql <= il and ir <= qr:
    return this.dat[i]
  if qr <= il or ir <= ql:
    return this.e
  let m = (il + ir) div 2
  return this.multiply(
    query(this, ql, qr, i * 2 + 1, il, m),
    query(this, ql, qr, i * 2 + 2, m, ir))

proc query[T](this: SegmentTree[T], ql, qr: int): T =
  return query(this, ql, qr, 0, 0, this.n)

let read = iterator: string {.closure.} =
  for s in stdin.readAll.split:
    yield s

proc main() =
  let
    n = read().parseInt
    a = newSeqWith(n, read().parseBiggestInt)

  var seg = initSegmentTree[int64](
    n,
    0,
    proc(x, y: int64): int64 = gcd(x, y))
  for i in 0..<n:
    seg.update(i, a[i])
  var ans: int64 = 0
  for i in 0..<n:
    if seg.query(i, n) > 1:
      continue
    var
      ng = i - 1
      ok = n
    # gcd(a[i], ..., a[ok - 1]) = 1
    # gcd(a[i], ..., a[ok - 1], ..., a[n - 1]) = 1
    while ok - ng > 1:
      let md = (ok + ng) div 2
      if seg.query(i, md) == 1:
        ok = md
      else:
        ng = md
    ans += n - ok + 1
  echo ans
main()
0