Skip to content

Commit d5855b7

Browse files
committed
p1990
1 parent 37075ab commit d5855b7

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

misc/luogu/p1990.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package main
2+
3+
import (
4+
. "fmt"
5+
"io"
6+
)
7+
8+
// https://space.bilibili.com/206214
9+
type matrix [][]int
10+
11+
func newMatrix(n, m int) matrix {
12+
a := make(matrix, n)
13+
for i := range a {
14+
a[i] = make([]int, m)
15+
}
16+
return a
17+
}
18+
19+
func (a matrix) mul(b matrix) matrix {
20+
c := newMatrix(len(a), len(b[0]))
21+
for i, row := range a {
22+
for k, x := range row {
23+
if x == 0 {
24+
continue
25+
}
26+
for j, y := range b[k] {
27+
c[i][j] = (c[i][j] + x*y) % 10000
28+
}
29+
}
30+
}
31+
return c
32+
}
33+
34+
func (a matrix) powMul(n int, f matrix) matrix {
35+
res := f
36+
for ; n > 0; n /= 2 {
37+
if n%2 > 0 {
38+
res = a.mul(res)
39+
}
40+
a = a.mul(a)
41+
}
42+
return res
43+
}
44+
45+
func p1990(in io.Reader, out io.Writer) {
46+
var n int
47+
Fscan(in, &n)
48+
if n == 1 {
49+
Fprint(out, 1)
50+
return
51+
}
52+
f2 := matrix{{2}, {1}, {1}}
53+
m := matrix{
54+
{2, 0, 1},
55+
{1, 0, 0},
56+
{0, 1, 0},
57+
};
58+
Fprint(out, m.powMul(n-2, f2)[0][0])
59+
}
60+
61+
//func main() { p1990(bufio.NewReader(os.Stdin), os.Stdout) }

misc/luogu/p1990_test.go

+18
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)