結果

問題 No.1036 Make One With GCD 2
ユーザー ikdikd
提出日時 2020-04-25 17:09:12
言語 Nim
(2.0.2)
結果
AC  
実行時間 876 ms / 2,000 ms
コード長 1,485 bytes
コンパイル時間 5,897 ms
コンパイル使用メモリ 70,760 KB
実行使用メモリ 38,704 KB
最終ジャッジ日時 2023-10-14 19:54:57
合計ジャッジ時間 26,401 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 724 ms
38,120 KB
testcase_01 AC 590 ms
34,664 KB
testcase_02 AC 424 ms
37,848 KB
testcase_03 AC 121 ms
13,676 KB
testcase_04 AC 221 ms
24,372 KB
testcase_05 AC 2 ms
4,356 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 204 ms
19,632 KB
testcase_08 AC 166 ms
17,092 KB
testcase_09 AC 618 ms
32,772 KB
testcase_10 AC 563 ms
32,484 KB
testcase_11 AC 618 ms
34,992 KB
testcase_12 AC 569 ms
32,512 KB
testcase_13 AC 702 ms
37,024 KB
testcase_14 AC 708 ms
37,976 KB
testcase_15 AC 663 ms
36,888 KB
testcase_16 AC 668 ms
36,852 KB
testcase_17 AC 687 ms
36,968 KB
testcase_18 AC 2 ms
4,348 KB
testcase_19 AC 4 ms
4,348 KB
testcase_20 AC 5 ms
4,352 KB
testcase_21 AC 5 ms
4,352 KB
testcase_22 AC 654 ms
37,152 KB
testcase_23 AC 483 ms
35,016 KB
testcase_24 AC 679 ms
37,456 KB
testcase_25 AC 622 ms
37,280 KB
testcase_26 AC 643 ms
37,172 KB
testcase_27 AC 2 ms
4,352 KB
testcase_28 AC 1 ms
4,348 KB
testcase_29 AC 2 ms
4,352 KB
testcase_30 AC 1 ms
4,356 KB
testcase_31 AC 2 ms
4,352 KB
testcase_32 AC 2 ms
4,352 KB
testcase_33 AC 1 ms
4,356 KB
testcase_34 AC 2 ms
4,352 KB
testcase_35 AC 2 ms
4,348 KB
testcase_36 AC 2 ms
4,348 KB
testcase_37 AC 1 ms
4,348 KB
testcase_38 AC 580 ms
37,692 KB
testcase_39 AC 692 ms
38,576 KB
testcase_40 AC 483 ms
35,020 KB
testcase_41 AC 841 ms
37,372 KB
testcase_42 AC 826 ms
38,704 KB
testcase_43 AC 840 ms
37,436 KB
testcase_44 AC 876 ms
37,876 KB
権限があれば一括ダウンロードができます

ソースコード

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
    le = 0
  for ri in 1..n:
    while le < ri and seg.query(le, ri) == 1:
      ans += n - ri + 1
      le += 1
  echo ans
main()
0