7.28 讲题 T1

· · 个人记录

我记得之前肯定有一道类似的题,并且我们做过。

首先,将复杂的图的结构转化一下:之前有一个虚拟节点连接所有点,现在把虚拟点去掉就行了,算一下两个点之间有多少条至少经过一个短路的路径。

现在知道 HP 可以游玩 n 天,所以我们可以 DP!DP 应该很显然吧,转移到其它点乘边权就行了。

但那个万恶的 HP 的寿命很长,可以游玩 10 亿天。幸运的是,阿米诺斯山上最多只有 200 个小屋。所以我们不能正常 DP。考虑那道类似的题,发现那题使用将邻接矩阵作为转移矩阵,这题的转移都是一模一样的,所以也可以矩阵乘法。实在不行推推样例。最后矩阵快速幂即可! ::::success[代码]

#include<bits/stdc++.h>
#define int long long
#define 114514 aaldkfjskldjfaslkdfj
#define using KK
#define namespace will
#define AK std
#define IOI ;

KK will AK IOI

const int N = 1e2 + 5, MOD = 1e9 + 7;

int n, m, ans;
int a[N], b[N];

struct Matrix{
    int n, m;
    int a[N][N];

    Matrix(){
        memset(a, 0, sizeof(a));
    }

    int *operator[](int i){
        return a[i];
    }

    const int *operator[](int i) const{
        return a[i];
    }
} F, E;

Matrix operator*(const Matrix &x, const Matrix &y){
    Matrix res;
    res.n = x.n, res.m = y.m;
    for(int k = 1; k <= x.m; k++){
        for(int i = 1; i <= x.n; i++){
            for(int j = 1; j <= y.m; j++){
                (res[i][j] += 1ll * x[i][k] * y[k][j] % MOD) %= MOD;
            }
        }
    }
    return res;
}

Matrix quickpow(Matrix a, int b){
    Matrix res = a;b--;
    while(b){
        if(b & 1) res = res * a;
        b >>= 1;
        a = a * a;
    }
    return res;
}

int main(){
    ios::sync_with_stdio(0), cin.tie(0);
    cin >> n >> m;
    F.n = 1;
    F.m = E.n = E.m = n;
    F[1][1] = 1;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) cin >> b[i];
    for(int i = 1; i <= n; i++){
        for(int j = 1; j <= n; j++){
            E[i][j] = (a[i] + b[i]) * (a[j] + b[j]) - b[i] * b[j];
        }
    }
    F = F * quickpow(E, m);
    for(int i = 1; i <= n; i++) (ans += F[1][i]) %= MOD;
    cout << ans;
    return 0;
}