結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 724 ms
38,736 KB
testcase_01 AC 595 ms
35,692 KB
testcase_02 AC 427 ms
37,792 KB
testcase_03 AC 121 ms
13,744 KB
testcase_04 AC 222 ms
24,516 KB
testcase_05 AC 2 ms
4,368 KB
testcase_06 AC 1 ms
4,368 KB
testcase_07 AC 205 ms
19,944 KB
testcase_08 AC 166 ms
17,116 KB
testcase_09 AC 604 ms
32,748 KB
testcase_10 AC 563 ms
32,464 KB
testcase_11 AC 615 ms
33,944 KB
testcase_12 AC 570 ms
32,544 KB
testcase_13 AC 700 ms
38,000 KB
testcase_14 AC 706 ms
38,044 KB
testcase_15 AC 661 ms
37,276 KB
testcase_16 AC 667 ms
36,624 KB
testcase_17 AC 687 ms
37,872 KB
testcase_18 AC 3 ms
4,372 KB
testcase_19 AC 3 ms
4,376 KB
testcase_20 AC 5 ms
4,368 KB
testcase_21 AC 5 ms
4,372 KB
testcase_22 AC 654 ms
36,804 KB
testcase_23 AC 485 ms
35,084 KB
testcase_24 AC 684 ms
36,920 KB
testcase_25 AC 622 ms
36,472 KB
testcase_26 AC 646 ms
36,400 KB
testcase_27 AC 2 ms
4,368 KB
testcase_28 AC 2 ms
4,368 KB
testcase_29 AC 1 ms
4,368 KB
testcase_30 AC 2 ms
4,372 KB
testcase_31 AC 2 ms
4,372 KB
testcase_32 AC 2 ms
4,372 KB
testcase_33 AC 2 ms
4,368 KB
testcase_34 AC 2 ms
4,368 KB
testcase_35 AC 2 ms
4,372 KB
testcase_36 AC 1 ms
4,368 KB
testcase_37 AC 2 ms
4,372 KB
testcase_38 AC 581 ms
38,224 KB
testcase_39 AC 692 ms
38,000 KB
testcase_40 AC 483 ms
34,900 KB
testcase_41 AC 839 ms
38,592 KB
testcase_42 AC 826 ms
37,556 KB
testcase_43 AC 840 ms
38,176 KB
testcase_44 AC 876 ms
38,736 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(
    this.query(ql, qr, i * 2 + 1, il, m),
    this.query(ql, qr, i * 2 + 2, m, ir))

proc query[T](this: SegmentTree[T], ql, qr: int): T =
  return this.query(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