結果
問題 |
No.826 連絡網
|
ユーザー |
|
提出日時 | 2019-05-20 14:09:02 |
言語 | Nim (2.2.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,444 bytes |
コンパイル時間 | 6,556 ms |
コンパイル使用メモリ | 284,260 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-02 01:38:34 |
合計ジャッジ時間 | 7,533 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 WA * 1 |
other | AC * 30 |
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]
ソースコード
import sequtils,math,times template stopwatch(body) = (let t1 = cpuTime();body;stderr.writeLine "TIME:",(cpuTime() - t1) * 1000,"ms") 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() stopwatch: let n = scan() let p = scan() var F = newUnionFind[int](n+1) var notIsPrimes = newSeq[bool](n+1) stopwatch: for i in 2..n.float.sqrt.int: if notIsPrimes[i] : continue for j in countup(i*2,n,i): F.merge(i,j) notIsPrimes[j] = true for i in n.float.sqrt.int..n div 2: if notIsPrimes[i]: continue F.merge(2,i) echo F.parent # echo F.parent[p] stopwatch: echo F.count(p) ]# # 結果を見て、0,1,[2....] proc getIsNotPrimes(n:int) :seq[bool] = # [0...n] O(n loglog n) result = newSeq[bool](n+1) result[0] = true result[1] = true for i in countup(2,n.float.sqrt.int): if result[i]: continue for j in countup(i*2,n,i): result[j] = true const isNotPrimes = getIsNotPrimes(1000_010) let n = scan() let p = scan() if p == 1 : quit "1",0 if p >= n div 2 and not isNotPrimes[p]: quit "1",0 var ans = n div 2 - 1 for i in countup(n div 2 + 1,n): if isNotPrimes[i] : ans += 1 echo ans