結果

問題 No.826 連絡網
ユーザー むらためむらため
提出日時 2019-05-20 05:06:22
言語 Nim
(2.0.2)
結果
AC  
実行時間 72 ms / 2,000 ms
コード長 1,729 bytes
コンパイル時間 2,446 ms
コンパイル使用メモリ 67,432 KB
実行使用メモリ 11,884 KB
最終ジャッジ日時 2023-09-14 18:35:54
合計ジャッジ時間 4,323 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 3 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 48 ms
9,496 KB
testcase_13 AC 19 ms
5,548 KB
testcase_14 AC 35 ms
7,732 KB
testcase_15 AC 5 ms
4,380 KB
testcase_16 AC 25 ms
6,288 KB
testcase_17 AC 20 ms
5,476 KB
testcase_18 AC 16 ms
4,964 KB
testcase_19 AC 58 ms
10,472 KB
testcase_20 AC 57 ms
10,256 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 20 ms
5,536 KB
testcase_23 AC 26 ms
6,284 KB
testcase_24 AC 12 ms
4,388 KB
testcase_25 AC 72 ms
11,884 KB
testcase_26 AC 14 ms
4,712 KB
testcase_27 AC 50 ms
9,564 KB
testcase_28 AC 36 ms
8,112 KB
testcase_29 AC 19 ms
5,520 KB
testcase_30 AC 71 ms
11,840 KB
testcase_31 AC 23 ms
6,148 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sequtils,math
template times*(n:int,body) = (for _ in 0..<n: body)
template `max=`*(x,y) = x = max(x,y)
template `min=`*(x,y) = x = min(x,y)
proc getchar_unlocked():char {. importc:"getchar_unlocked",header: "<stdio.h>" ,discardable.}
proc scan(): int =
  while true:
    let k = getchar_unlocked()
    if k < '0': return
    result = 10 * result + k.ord - '0'.ord
template useUnionFind() = # 同一集合の判定/マージ が 実質 O(1)
  type UnionFind[T] = ref object
    parent : seq[T]
  proc newUnionFind[T](size:int) : UnionFind[T] =
    new(result)
    result.parent = newSeqUninitialized[T](size)
    for i in 0.int32..<size.int32: result.parent[i] = i
  proc root[T](self:var UnionFind[T],x:T): T =
    if self.parent[x] == x: return x
    self.parent[x] = self.root(self.parent[x])
    return self.parent[x]
  proc same[T](self:var UnionFind[T],x,y:T) : bool = self.root(x) == self.root(y)
  proc merge[T](self:var UnionFind[T],sx,sy:T) : bool {.discardable.} =
    var rx = self.root(sx)
    var ry = self.root(sy)
    if rx == ry : return false
    if self.parent[ry] < self.parent[rx] : swap(rx,ry)
    if self.parent[rx] == self.parent[ry] : self.parent[rx] -= 1
    self.parent[ry] = rx
    return true
  proc count[T](self:var UnionFind[T],x:T):int =
    for p in self.parent:(if p == self.parent[x]: result += 1)
useUnionFind()

let n = scan()
let p = scan()
var F = newUnionFind[int](n+1)
var isPrimes = newSeqWith(n+1,true)
for i in 2..n.float.sqrt.int:
  if not isPrimes[i] : continue
  for j in countup(i,n,i):
    F.merge(i,j)
    if i != j : isPrimes[j] = false
for i in n.float.sqrt.int..n div 2:
  if not isPrimes[i]: continue
  F.merge(2,i)
# echo F.parent
# echo F.parent[p]
echo F.count(p)
0