-
Notifications
You must be signed in to change notification settings - Fork 674
/
Copy pathp1939.go
68 lines (61 loc) · 1.04 KB
/
p1939.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
package main
import (
. "fmt"
"io"
)
// https://space.bilibili.com/206214
type matrix1939 [][]int
func newMatrix1939(n, m int) matrix1939 {
a := make(matrix1939, n)
for i := range a {
a[i] = make([]int, m)
}
return a
}
func (a matrix1939) mul(b matrix1939) matrix1939 {
c := newMatrix1939(len(a), len(b[0]))
for i, row := range a {
for k, x := range row {
if x == 0 {
continue
}
for j, y := range b[k] {
c[i][j] = (c[i][j] + x*y) % 1_000_000_007
}
}
}
return c
}
// a^n * f0
func (a matrix1939) powMul(n int, f0 matrix1939) matrix1939 {
res := f0
for ; n > 0; n /= 2 {
if n%2 > 0 {
res = a.mul(res)
}
a = a.mul(a)
}
return res
}
func p1939(in io.Reader, out io.Writer) {
var T, n int
for Fscan(in, &T); T > 0; T-- {
Fscan(in, &n)
if n <= 3 {
Fprintln(out, 1)
continue
}
m := matrix1939{
{1, 0, 1},
{1, 0, 0},
{0, 1, 0},
}
f0 := matrix1939{
{1},
{1},
{1},
}
Fprintln(out, m.powMul(n-3, f0)[0][0])
}
}
//func main() { p1939(bufio.NewReader(os.Stdin), os.Stdout) }