-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy path153.go
More file actions
69 lines (60 loc) · 1.05 KB
/
153.go
File metadata and controls
69 lines (60 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// UVa 153 - Permalex
package main
import (
"fmt"
"math/big"
"os"
)
func factorial(a int) *big.Int {
f := big.NewInt(1)
for ; a > 1; a-- {
f.Mul(f, big.NewInt(int64(a)))
}
return f
}
func calc(a, b int) *big.Int {
f := factorial(a)
return f.Div(f, factorial(b))
}
func solve(word string) *big.Int {
if len(word) <= 1 {
return big.NewInt(1)
}
smallerMap := make(map[byte]bool)
for i := 1; i < len(word); i++ {
if word[0] > word[i] {
smallerMap[word[i]] = true
}
}
charMap := make(map[byte]int)
for i := 1; i < len(word); i++ {
charMap[word[i]]++
}
total := big.NewInt(0)
for i := range smallerMap {
dup := 0
for j, v := range charMap {
if j == i {
v--
}
if v > 1 {
dup += v
}
}
total.Add(total, calc(len(word)-1, dup))
}
return total.Add(total, solve(word[1:]))
}
func main() {
in, _ := os.Open("153.in")
defer in.Close()
out, _ := os.Create("153.out")
defer out.Close()
var word string
for {
if fmt.Fscanf(in, "%s", &word); word == "#" {
break
}
fmt.Fprintf(out, "%10v\n", solve(word))
}
}