-
Notifications
You must be signed in to change notification settings - Fork 0
/
p74.go
83 lines (67 loc) · 1.12 KB
/
p74.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"time"
)
func Next(frac []int, curr int) int {
val := 0
for curr > 0 {
val += frac[curr%10]
curr /= 10
}
return val
}
func Chain(frac, dp []int, curr int) int {
chain := []int{curr}
next := curr
for {
next = Next(frac, next)
for _, pre := range chain {
if next == pre {
dp[curr] = len(chain)
for i, x := range chain {
if x < 1000000 {
dp[x] = dp[curr] - i
}
}
return dp[curr]
}
}
if next < 1000000 && dp[next] != 0 {
dp[curr] = len(chain) + dp[next]
for i, x := range chain {
if x < 1000000 {
dp[x] = dp[curr] - i
}
}
return dp[curr]
}
chain = append(chain, next)
}
return dp[curr]
}
func main() {
start := time.Now()
frac := make([]int, 10)
frac[0] = 1
for i := 1; i < 10; i++ {
frac[i] = frac[i-1] * i
}
dp := make([]int, 1000000)
dp[0] = 2
dp[1] = 1
dp[2] = 1
ans := 0
for i := 3; i < 1000000; i++ {
if dp[i] == 0 {
if Chain(frac, dp, i) == 60 {
ans++
}
} else if dp[i] == 60 {
ans++
}
}
fmt.Println(ans)
elapsed := time.Since(start)
fmt.Println("Elasped:", elapsed)
}