結果

問題 No.827 総神童数
ユーザー aru aruaru aru
提出日時 2020-11-14 11:21:31
言語 Go
(1.22.1)
結果
AC  
実行時間 169 ms / 2,000 ms
コード長 3,565 bytes
コンパイル時間 14,539 ms
コンパイル使用メモリ 203,444 KB
実行使用メモリ 42,956 KB
最終ジャッジ日時 2023-09-30 04:48:26
合計ジャッジ時間 21,399 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,356 KB
testcase_01 AC 1 ms
4,356 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 1 ms
4,360 KB
testcase_04 AC 1 ms
4,356 KB
testcase_05 AC 2 ms
4,360 KB
testcase_06 AC 2 ms
4,356 KB
testcase_07 AC 1 ms
4,352 KB
testcase_08 AC 1 ms
4,356 KB
testcase_09 AC 169 ms
42,956 KB
testcase_10 AC 53 ms
15,860 KB
testcase_11 AC 3 ms
4,352 KB
testcase_12 AC 20 ms
9,116 KB
testcase_13 AC 141 ms
40,768 KB
testcase_14 AC 5 ms
4,356 KB
testcase_15 AC 42 ms
11,724 KB
testcase_16 AC 130 ms
28,460 KB
testcase_17 AC 152 ms
30,632 KB
testcase_18 AC 66 ms
22,224 KB
testcase_19 AC 137 ms
14,292 KB
testcase_20 AC 93 ms
12,084 KB
testcase_21 AC 63 ms
9,972 KB
testcase_22 AC 103 ms
12,176 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 118 ms
14,260 KB
testcase_25 AC 83 ms
12,064 KB
testcase_26 AC 116 ms
14,264 KB
testcase_27 AC 71 ms
9,992 KB
testcase_28 AC 71 ms
9,992 KB
testcase_29 AC 55 ms
7,912 KB
testcase_30 AC 14 ms
5,500 KB
testcase_31 AC 41 ms
7,888 KB
testcase_32 AC 39 ms
7,808 KB
testcase_33 AC 135 ms
14,284 KB
testcase_34 AC 118 ms
14,256 KB
testcase_35 AC 42 ms
7,888 KB
testcase_36 AC 62 ms
9,972 KB
testcase_37 AC 81 ms
12,064 KB
testcase_38 AC 126 ms
14,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package main

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

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

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

func getF() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

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

func getS() 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
}

var N int
var node [][]int
var dist []int

func dfs(v, p, cnt int) {
	dist[v] = cnt
	for _, e := range node[v] {
		if e == p {
			continue
		}
		dfs(e, v, cnt+1)
	}
}

//----------------------------------------
// modint
//----------------------------------------
type modint struct {
	mod int
}

func newModint(m int) *modint {
	var ret modint
	ret.mod = m
	return &ret
}

func (m *modint) add(a, b int) int {
	ret := (a + b) % m.mod
	if ret < 0 {
		ret += m.mod
	}
	return ret
}

func (m *modint) sub(a, b int) int {
	ret := (a - b) % m.mod
	if ret < 0 {
		ret += m.mod
	}
	return ret
}

func (m *modint) mul(a, b int) int {
	ret := a * b % m.mod
	if ret < 0 {
		ret += m.mod
	}
	return ret
}

func (m *modint) div(a, b int) int {
	ret := a * m.modinv(b)
	ret %= m.mod
	return ret
}

func (m *modint) pow(p, n int) int {
	ret := 1
	x := p
	for n != 0 {
		if n%2 == 1 {
			ret *= x
			ret %= m.mod
		}
		n /= 2
		x = x * x % m.mod
	}
	return ret
}

func (m *modint) mulMod(A, B [][]int) [][]int {
	H := len(A)
	W := len(B[0])
	K := len(A[0])
	C := make([][]int, W)
	for i := 0; i < W; i++ {
		C[i] = make([]int, W)
	}

	for i := 0; i < H; i++ {
		for j := 0; j < W; j++ {
			for k := 0; k < K; k++ {
				C[i][j] += A[i][k] * B[k][j]
				C[i][j] %= m.mod
			}
		}
	}

	return C
}

// A[][]のp乗を求める
func (m *modint) powModMatrix(A [][]int, p int) [][]int {
	N := len(A)
	ret := make([][]int, N)
	for i := 0; i < N; i++ {
		ret[i] = make([]int, N)
		ret[i][i] = 1
	}

	for p > 0 {
		if p&1 == 1 {
			ret = m.mulMod(ret, A)
		}
		A = m.mulMod(A, A)
		p >>= 1
	}

	return ret
}

// 逆元を使った割り算(MOD)
// mod. m での a の逆元 a^{-1} を計算する
func (m *modint) modinv(a int) int {
	b := m.mod
	u := 1
	v := 0
	for b != 0 {
		t := a / b
		a -= t * b
		a, b = b, a
		u -= t * v
		u, v = v, u
	}
	u %= m.mod
	if u < 0 {
		u += m.mod
	}
	return u
}

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, 1000000)
	// this template is new version.
	// use getI(), getS(), getInts(), getF()
	N = getI()
	node = make([][]int, N)
	for i := 0; i < N-1; i++ {
		from, to := getI()-1, getI()-1
		node[from] = append(node[from], to)
		node[to] = append(node[to], from)
	}
	dist = make([]int, N)
	dfs(0, -1, 0)

	m := newModint(1e9 + 7)
	n := 1
	for i := 1; i <= N; i++ {
		n = m.mul(n, i)
	}
	ans := 0
	for i := 0; i < N; i++ {
		ans = m.add(ans, m.div(n, dist[i]+1))
	}
	out(ans)
}
0