bzoj 1875 矩阵快速幂

思路:不能走走过来的路,变点交换跑矩阵快速幂。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define y1 skldjfskldjg
#define y2 skldfjsklejg
using namespace std;

const int N = 1e5 + 7;
const int M = 5e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 45989;

int n, m, t, S, T, U[N], V[N], b[N];

struct Matrix {
    int a[125][125], n;

    Matrix(int _n) {
        n = _n;
        memset(a, 0, sizeof(a));
    }

    void init() {
        for(int i = 0; i < n; i++)
            a[i][i] = 1;
    }

    Matrix operator * (const Matrix &B) const {
        Matrix C(n);
        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                for(int k = 0; k < n; k++)
                    C.a[i][j] = (C.a[i][j] + a[i][k] * B.a[k][j]) % mod;
        return C;
    }  

    Matrix operator ^ (int b) {
        Matrix A = (*this);
        Matrix ans(n);
        ans.init();
        while(b) {
            if(b & 1) ans = ans * A;
            A = A * A; b >>= 1;
        }
        return ans;
    }
};
int main() {
    scanf("%d%d%d%d%d", &n, &m, &t, &S, &T);
    for(int i = 1; i <= m; i++) {
        scanf("%d%d", &U[i << 1], &V[i << 1]);
        U[i << 1 | 1] = V[i << 1];
        V[i << 1 | 1] = U[i << 1];
    }
    m = m << 1 | 1;
    Matrix A(m + 1);
    for(int i = 2; i <= m; i++) {
        if(U[i] == S) A.a[0][i] = 1;
        if(V[i] == T) A.a[i][1] = 1;
        for(int j = 2; j <= m; j++)
            if(V[i] == U[j] && (i ^ j) != 1)
                A.a[i][j] = 1;
    }
    A = A ^ (t + 1);
    printf("%d\n", A.a[0][1]);
    return 0;
}

 

posted @ 2018-09-06 17:21  NotNight  阅读(83)  评论(0编辑  收藏  举报