跳到正文

题解

题解:P11765 「KFCOI Round #1」回首

wurang约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年6月21日
收录
2026年7月22日
更新
2026年11月11日
篇幅
约 5 分钟
获赞
2
题目
P11765

版本

  1. v1当前

这道题目一看数据范围 n≤100,1≤T≤1018n \le 100,1 \le T \le 10^{18},就能想到使用矩阵快速幂求解。

这个矩阵不仅要存储每两个点之间的转移,还要记录第 tt 次操作中的 tt,由于每次 tt 会加一,所以还要多一维记录常量 11。

记本轮为第 tt 次,第 ii 次后第 jj 个点的重要系数为 dpi,jdp_{i,j},ei,je_{i,j} 表示是否有一条从 ii 到 jj 的边,fif_i 表示第 ii 个点的入读是否为 00,容易得到转移矩阵。

[dpt−1,1dpt−1,2⋯dpt−1,100t1][e1,1e1,2⋯e1,10000e2,1e2,2⋯e2,10000⋮⋮⋱⋮⋮⋮e100,1e100,2⋯e100,10000f1f2⋯f1001000⋯011]=[dpt,1dpt,2⋯dpt,100t+11]\begin{bmatrix} dp_{t-1,1} & dp_{t-1,2} & \cdots & dp_{t-1,100} & t & 1 \end{bmatrix} \begin{bmatrix} e_{1,1} & e_{1,2} & \cdots & e_{1,100} & 0 & 0 \\ e_{2,1} & e_{2,2} & \cdots & e_{2,100} & 0 & 0 \\ \vdots & \vdots & \ddots & \vdots & \vdots & \vdots\\ e_{100,1} & e_{100,2} & \cdots & e_{100,100} & 0 & 0 \\ f_1 & f_2 & \cdots & f_{100} & 1 & 0 0 & 0 \cdots & 0 & 1 & 1 \end{bmatrix} = \begin{bmatrix} dp_{t,1} & dp_{t,2} & \cdots & dp_{t,100} & t+1 & 1 \end{bmatrix}

初始矩阵为:

[00⋯011]\begin{bmatrix} 0 & 0 & \cdots & 0 & 1 & 1 \end{bmatrix}

最后用矩阵快速幂计算即可。

代码:

C++
#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;
}
原文
存档评论1 条抓取于