結果
問題 | No.827 総神童数 |
ユーザー | ccppjsrb |
提出日時 | 2020-08-18 11:02:18 |
言語 | Go (1.22.1) |
結果 |
AC
|
実行時間 | 149 ms / 2,000 ms |
コード長 | 3,336 bytes |
コンパイル時間 | 16,180 ms |
コンパイル使用メモリ | 229,980 KB |
実行使用メモリ | 46,044 KB |
最終ジャッジ日時 | 2024-10-12 02:26:03 |
合計ジャッジ時間 | 19,456 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1 ms
6,820 KB |
testcase_01 | AC | 2 ms
6,816 KB |
testcase_02 | AC | 1 ms
6,816 KB |
testcase_03 | AC | 2 ms
6,816 KB |
testcase_04 | AC | 2 ms
6,820 KB |
testcase_05 | AC | 2 ms
6,820 KB |
testcase_06 | AC | 1 ms
6,820 KB |
testcase_07 | AC | 2 ms
6,820 KB |
testcase_08 | AC | 1 ms
6,820 KB |
testcase_09 | AC | 147 ms
46,044 KB |
testcase_10 | AC | 42 ms
15,104 KB |
testcase_11 | AC | 3 ms
5,248 KB |
testcase_12 | AC | 18 ms
8,192 KB |
testcase_13 | AC | 123 ms
41,472 KB |
testcase_14 | AC | 5 ms
5,248 KB |
testcase_15 | AC | 38 ms
15,104 KB |
testcase_16 | AC | 102 ms
29,440 KB |
testcase_17 | AC | 117 ms
33,152 KB |
testcase_18 | AC | 52 ms
21,760 KB |
testcase_19 | AC | 149 ms
21,376 KB |
testcase_20 | AC | 99 ms
16,896 KB |
testcase_21 | AC | 73 ms
13,568 KB |
testcase_22 | AC | 114 ms
18,560 KB |
testcase_23 | AC | 2 ms
5,248 KB |
testcase_24 | AC | 126 ms
19,584 KB |
testcase_25 | AC | 96 ms
16,000 KB |
testcase_26 | AC | 129 ms
19,712 KB |
testcase_27 | AC | 82 ms
14,592 KB |
testcase_28 | AC | 82 ms
14,592 KB |
testcase_29 | AC | 61 ms
12,288 KB |
testcase_30 | AC | 15 ms
5,248 KB |
testcase_31 | AC | 43 ms
9,984 KB |
testcase_32 | AC | 43 ms
9,600 KB |
testcase_33 | AC | 143 ms
20,992 KB |
testcase_34 | AC | 127 ms
19,712 KB |
testcase_35 | AC | 49 ms
10,112 KB |
testcase_36 | AC | 74 ms
13,440 KB |
testcase_37 | AC | 98 ms
16,000 KB |
testcase_38 | AC | 129 ms
20,608 KB |
ソースコード
package main import ( "bufio" "fmt" "os" "strconv" ) func configure(scanner *bufio.Scanner) { scanner.Split(bufio.ScanWords) scanner.Buffer(make([]byte, 1000005), 1000005) } func getNextString(scanner *bufio.Scanner) string { scanned := scanner.Scan() if !scanned { panic("scan failed") } return scanner.Text() } func getNextInt(scanner *bufio.Scanner) int { i, _ := strconv.Atoi(getNextString(scanner)) return i } func getNextInt64(scanner *bufio.Scanner) int64 { i, _ := strconv.ParseInt(getNextString(scanner), 10, 64) return i } func getNextFloat64(scanner *bufio.Scanner) float64 { i, _ := strconv.ParseFloat(getNextString(scanner), 64) return i } func main() { fp := os.Stdin wfp := os.Stdout extra := 0 if os.Getenv("I") == "IronMan" { fp, _ = os.Open(os.Getenv("END_GAME")) extra = 100 } scanner := bufio.NewScanner(fp) configure(scanner) writer := bufio.NewWriter(wfp) defer func() { r := recover() if r != nil { fmt.Fprintln(writer, r) } writer.Flush() }() solve(scanner, writer) for i := 0; i < extra; i++ { fmt.Fprintln(writer, "-----------------------------------") solve(scanner, writer) } } func solve(scanner *bufio.Scanner, writer *bufio.Writer) { n := getNextInt(scanner) g := newGraph(n) for i := 0; i < n-1; i++ { u := getNextInt(scanner) - 1 v := getNextInt(scanner) - 1 g.appendEdge(u, v, i) g.appendEdge(v, u, i) } g.dfs(0, -1, 1) mc := newModcom(n) var ans mint for i := 0; i < n; i++ { ans.addAs(mc.choose(n, g.v[i].d).mul(mc.facs[g.v[i].d-1]).mul(mc.facs[n-g.v[i].d])) } fmt.Fprintln(writer, ans) } type vertex struct { d int } type edge struct { to, id int } type graph struct { v []vertex e [][]edge } func newGraph(n int) graph { return graph{ v: make([]vertex, n), e: make([][]edge, n), } } func (g *graph) appendEdge(from, to, id int) { g.e[from] = append(g.e[from], edge{ to: to, id: id, }) } func (g *graph) dfs(i, p int, d int) { g.v[i].d = d for _, e := range g.e[i] { if e.to == p { continue } g.dfs(e.to, i, d+1) } } type mint int64 func (mt mint) mod() mint { m := mint(1e9 + 7) mt %= m if mt < 0 { return mt + m } return mt } func (mt mint) inv() mint { return mt.pow(mint(0).sub(2)) } func (mt mint) pow(n mint) mint { p := mint(1) for n > 0 { if n&1 == 1 { p.mulAs(mt) } mt.mulAs(mt) n >>= 1 } return p } func (mt mint) add(x mint) mint { return (mt + x).mod() } func (mt mint) sub(x mint) mint { return (mt - x).mod() } func (mt mint) mul(x mint) mint { return (mt * x).mod() } func (mt mint) div(x mint) mint { return mt.mul(x.inv()) } func (mt *mint) addAs(x mint) *mint { *mt = mt.add(x) return mt } func (mt *mint) subAs(x mint) *mint { *mt = mt.sub(x) return mt } func (mt *mint) mulAs(x mint) *mint { *mt = mt.mul(x) return mt } func (mt *mint) divAs(x mint) *mint { *mt = mt.div(x) return mt } type modcom struct { facs, invs []mint } func newModcom(n int) modcom { facs := make([]mint, n+1) invs := make([]mint, n+1) facs[0] = 1 for i := 0; i < n; i++ { facs[i+1] = facs[i].mul(mint(i + 1)) } invs[n] = facs[n].inv() for i := n - 1; i >= 0; i-- { invs[i] = invs[i+1].mul(mint(i + 1)) } return modcom{ facs: facs, invs: invs, } } func (mc *modcom) choose(n, k int) mint { return mc.facs[n].mul(mc.invs[k]).mul(mc.invs[n-k]) }