-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathModInt.go
128 lines (115 loc) · 2.85 KB
/
ModInt.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
)
var iost *Iost
type Iost struct {
Scanner *bufio.Scanner
Writer *bufio.Writer
}
func NewIost(fp io.Reader, wfp io.Writer) *Iost {
const BufSize = 2000005
scanner := bufio.NewScanner(fp)
scanner.Split(bufio.ScanWords)
scanner.Buffer(make([]byte, BufSize), BufSize)
return &Iost{Scanner: scanner, Writer: bufio.NewWriter(wfp)}
}
func (i *Iost) Text() string {
if !i.Scanner.Scan() {
panic("scan failed")
}
return i.Scanner.Text()
}
func (i *Iost) Atoi(s string) int { x, _ := strconv.Atoi(s); return x }
func (i *Iost) GetNextInt() int { return i.Atoi(i.Text()) }
func (i *Iost) Atoi64(s string) int64 { x, _ := strconv.ParseInt(s, 10, 64); return x }
func (i *Iost) GetNextInt64() int64 { return i.Atoi64(i.Text()) }
func (i *Iost) Atof64(s string) float64 { x, _ := strconv.ParseFloat(s, 64); return x }
func (i *Iost) GetNextFloat64() float64 { return i.Atof64(i.Text()) }
func (i *Iost) Print(x ...interface{}) { fmt.Fprint(i.Writer, x...) }
func (i *Iost) Printf(s string, x ...interface{}) { fmt.Fprintf(i.Writer, s, x...) }
func (i *Iost) Println(x ...interface{}) { fmt.Fprintln(i.Writer, x...) }
func main() {
fp := os.Stdin
wfp := os.Stdout
iost = NewIost(fp, wfp)
defer func() {
iost.Writer.Flush()
}()
solve()
}
func solve() {
// https://atcoder.jp/contests/abc291/tasks/abc291_d
// dp[i][0/1] : 当前看到第i个点, 之前那张牌是否翻转
n := iost.GetNextInt()
cards := make([][2]int, n)
for i := 0; i < n; i++ {
cards[i][0] = iost.GetNextInt()
cards[i][1] = iost.GetNextInt()
}
dp := make([][2]ModInt, n)
dp[0][0] = 1
dp[0][1] = 1
for i := 1; i < n; i++ {
for pre := 0; pre < 2; pre++ {
for cur := 0; cur < 2; cur++ {
if cards[i-1][pre] != cards[i][cur] {
dp[i][cur].IAdd(dp[i-1][pre])
}
}
}
}
iost.Println(dp[n-1][0].Add(dp[n-1][1]))
}
const MOD = 998244353
type ModInt int64
func (m ModInt) Add(x ModInt) ModInt {
return (m + x).mod()
}
func (m *ModInt) IAdd(x ModInt) {
*m = m.Add(x)
}
func (m ModInt) Sub(x ModInt) ModInt {
return (m - x).mod()
}
func (m *ModInt) ISub(x ModInt) {
*m = m.Sub(x)
}
func (m ModInt) Mul(x ModInt) ModInt {
return (m * x).mod()
}
func (m *ModInt) IMul(x ModInt) {
*m = m.Mul(x)
}
func (m ModInt) Div(x ModInt) ModInt {
return m.Mul(x.Inv())
}
func (m *ModInt) IDiv(x ModInt) {
*m = m.Div(x)
}
func (m ModInt) Pow(n ModInt) ModInt {
m = m.mod()
p := ModInt(1)
for n > 0 {
if n&1 == 1 {
p.IMul(m)
}
m.IMul(m)
n >>= 1
}
return p
}
func (m ModInt) Inv() ModInt {
return m.Pow(ModInt(0).Sub(2))
}
func (m ModInt) mod() ModInt {
m %= MOD
if m < 0 {
m += MOD
}
return m
}