結果

問題 No.826 連絡網
ユーザー aru aruaru aru
提出日時 2020-10-08 20:46:37
言語 Go
(1.22.1)
結果
AC  
実行時間 222 ms / 2,000 ms
コード長 1,821 bytes
コンパイル時間 13,696 ms
コンパイル使用メモリ 202,296 KB
実行使用メモリ 11,936 KB
最終ジャッジ日時 2023-09-27 11:59:30
合計ジャッジ時間 13,427 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 1 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 145 ms
9,848 KB
testcase_13 AC 49 ms
5,536 KB
testcase_14 AC 102 ms
7,776 KB
testcase_15 AC 8 ms
4,376 KB
testcase_16 AC 64 ms
5,548 KB
testcase_17 AC 50 ms
5,532 KB
testcase_18 AC 36 ms
5,524 KB
testcase_19 AC 179 ms
9,864 KB
testcase_20 AC 172 ms
9,860 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 50 ms
5,536 KB
testcase_23 AC 70 ms
5,548 KB
testcase_24 AC 25 ms
4,380 KB
testcase_25 AC 222 ms
11,932 KB
testcase_26 AC 32 ms
4,380 KB
testcase_27 AC 152 ms
9,848 KB
testcase_28 AC 116 ms
7,780 KB
testcase_29 AC 50 ms
5,536 KB
testcase_30 AC 222 ms
11,936 KB
testcase_31 AC 60 ms
5,544 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

import (
	"bufio"
	"fmt"
	"os"
	"sort"
	"strconv"
)

func out(x ...interface{}) {
	fmt.Println(x...)
}

var sc = bufio.NewScanner(os.Stdin)

func getInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func getInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = getInt()
	}
	return ret
}

func getString() string {
	sc.Scan()
	return sc.Text()
}

// min, max, asub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

func asub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func main() {
	sc.Split(bufio.ScanWords)
	N, P := getInt(), getInt()

	uf := newUnionFind(N + 1)
	for i := 2; i <= N; i++ {
		for j := i + i; j <= N; j += i {
			uf.unite(i, j)
		}
	}
	out(uf.size(P))
}

// UnionFind高速
type UnionFind struct {
	d []int
}

func newUnionFind(N int) *UnionFind {
	u := new(UnionFind)
	u.d = make([]int, N)
	for i := 0; i < N; i++ {
		u.d[i] = -1
	}
	return u
}

func (p *UnionFind) root(x int) int {
	if p.d[x] < 0 {
		return x
	}
	p.d[x] = p.root(p.d[x])
	return p.d[x]
}

func (p *UnionFind) unite(x, y int) bool {
	x = p.root(x)
	y = p.root(y)
	if x == y {
		return false
	}
	if p.d[x] > p.d[y] {
		x, y = y, x
	}
	p.d[x] += p.d[y]
	p.d[y] = x
	return true
}

func (p *UnionFind) same(x, y int) bool {
	return p.root(x) == p.root(y)
}

func (p *UnionFind) size(x int) int {
	return -p.d[p.root(x)]
}
0