题解:P11765 「KFCOI Round #1」回首
这道题目一看数据范围
这个矩阵不仅要存储每两个点之间的转移,还要记录第
记本轮为第
初始矩阵为:
最后用矩阵快速幂计算即可。
代码:
#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;}
const int mod = 998244353, sz = 105;
struct Matrix
{
int a[sz][sz];
Matrix()
{
for (int i = 0; i < sz; i++)
for (int j = 0; j < sz; j++)
a[i][j] = 0;
}
Matrix operator*(const Matrix &T) const
{
Matrix res;
int r;
for (int i = 0; i < sz; ++i)
for (int k = 0; k < sz; ++k)
{
r = a[i][k];
for (int j = 0; j < sz; ++j)
res.a[i][j] = (res.a[i][j] + T.a[k][j] * r) % mod;
}
return res;
}
Matrix operator^(int x) const
{
Matrix res, bas;
for (int i = 0; i < sz; ++i)
res.a[i][i] = 1;
for (int i = 0; i < sz; ++i)
for (int j = 0; j < sz; ++j)
bas.a[i][j] = a[i][j] % mod;
while (x)
{
if (x & 1)
res = res * bas;
bas = bas * bas;
x >>= 1;
}
return res;
}
}ans, mat;
int n, m, t;
int k, u, v;
bool f[105];
signed main()
{
cin >> n >> m >> t;
for(int i = 1; i <= n; i++)
cin >> k, mat.a[i][i] = k;
for(int i = 1; i <= m; i++)
{
cin >> u >> v;
mat.a[u][v] = 1;
f[v] = 1;
}
for(int i = 1; i <= n; i++)
if(!f[i]) mat.a[101][i] = 1;
mat.a[101][101] = mat.a[102][101] = 1;
mat.a[102][102] = 1;
ans.a[1][101] = 1, ans.a[1][102] = 1;
ans = ans * (mat ^ t);
for(int i = 1; i <= n; i++)
cout << ans.a[1][i] << " ";
return 0;
}