2019 ICPC南昌 J.Summon

在环上涂颜色,不能出现给定颜色的顺时针顺序。(长度为=4)。

变换一下就是

然后$f(d)$就表示,长度$d$的环符合 要求有几个(不用考虑选择)
刚开始觉得这题,用快速幂挺炫酷的,发现就是套路。转化成从某个状态走$d$步到某个状态的步数种类。

跟$POJ2888$一摸一样。

变成四进制。然后矩阵快速幂没了。

  • 注意预处理可以转移的进制。
  • 常数优化。
代码
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define pii pair<int, int>
#define mk make_pair
const int N = 1e3 + 10;
const int mod = 998244353;

const int Z = 64;
struct Matrix
{

int row, col;
int s[Z][Z];
Matrix()
{ //无参数构造函数
row = col = 0;
memset(s, 0, sizeof(s));
}
Matrix(int n, int m, int x = 0) : row(n), col(m)
{
memset(s, 0, sizeof(s));
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
s[i][j] = x;
} //重载构造函数

void printp()
{ //输出一个矩阵
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
cout << s[i][j] << " ";
cout << endl;
}
}
// void transpose()
// { //矩阵的转置
// Matrix y(col, row);
// for (int i = 1; i <= row; i++)
// {
// for (int j = 1; j <= col; j++)
// y.s[j][i] = s[i][j];
// }
// *this = y;
// }
void identity()
{ //将该矩阵改成单位矩阵
for (int i = 0; i < row; i++)
s[i][i] = 1;
}
// Matrix operator+(Matrix x)
// { //重载+
// Matrix y(row, col);
// for (int i = 0; i <row; i++)
// {
// for (int j = 0; j < col; j++)
// y.s[i][j] += s[i][j] + x.s[i][j], y.s[i][j] %= mod;
// }
// return y;
// }
Matrix operator*(Matrix x)
{ //重载*
Matrix y(row, x.col);
for (int i = 0; i < row; i++)
{
for (int j = 0; j < x.col; j++)
{
for (int k = 0; k < col; k++)
{
if (!s[i][k] || !x.s[k][j])
continue;
y.s[i][j] = (y.s[i][j] + 1ll * s[i][k] * x.s[k][j] % mod) % mod;
}
}
}
return y;
}
Matrix operator*=(Matrix x)
{ //重载*=
return *this = *this * x;
}
} z[40];
int qpow(int a, int x, int mo)
{
int res = 1;
while (x)
{
if (x & 1)
res = 1ll * res * a % mod;
a = 1ll * a * a % mod;
x >>= 1;
}
return res;
}

Matrix qpow(int x)
{
Matrix res(z[0].row, z[0].col);
res.identity();

for (int i = 0; (1 << i) <= x; i++)
{
if ((1 << i) & x)
{
res = res * z[i];
}
}

return res;
}

namespace Polya
{

int mult(int x, int y)
{
return 1ll * x * y % mod;
}
int calc(int x)
{
// dt.printp();
Matrix res = qpow(x);

int ans = 0;
for (int i = 0; i < 64; i++)
ans = (ans + res.s[i][i]) % mod;

return ans;
}
int p[N], e[N];
int cnt = 0;
int ans = 0;
void dfs(int x, int now, int phi, int n)
{
if (x > cnt)
{
ans = (ans + mult(phi % mod, calc(n / now))) % mod;

return;
}
dfs(x + 1, now, phi, n);
for (int i = 1; i <= e[x]; i++)
{
phi *= (p[x] - (i == 1));
now *= p[x];
dfs(x + 1, now, phi, n);
}
}
int solve(int n)
{
cnt = 0;
ans = 0;
int tmp = n;
for (int i = 2; i * i <= n; i++)
if (n % i == 0)
{
p[++cnt] = i;
e[cnt] = 0;
while (n % i == 0)
{
n /= i;
e[cnt]++;
}
}
if (n > 1)
{
p[++cnt] = n;
e[cnt] = 1;
}
dfs(1, 1, 1, tmp);
//cout << ans << endl;
return 1ll * ans * qpow(tmp % mod, mod - 2, mod) % mod;
}
} // namespace Polya

int read()
{
int x = 0, f = 1;
char c = getchar();
while (c < '0' || c > '9')
{
if (c == '-')
f = -1;
c = getchar();
}
while (c >= '0' && c <= '9')
x = (x << 1) + (x << 3) + c - '0', c = getchar();
return x * f;
}

int main()
{
int n = read(), m = read();
Matrix tmp(64, 64, 0);
for (int i = 0; i < 64; i++)
for (int j = 0; j < 64; j++)
{
if (((j << 2) & 63) == ((i >> 2) << 2))
tmp.s[i][j] = 1;
}
for (int i = 1; i <= m; i++)
{
vector<int> a(4);
for (int j = 0; j < 4; j++)
{
a[j] = read();
}
tmp.s[a[0] + (a[1] << 2) + (a[2] << 4)][a[1] + (a[2] << 2) + (a[3] << 4)] = 0;
}
z[0] = tmp;
for (int i = 1; (1 << i) <= n; i++)
z[i] = z[i - 1] * z[i - 1];

cout << Polya::solve(n) << endl;
}