欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

BZOJ1898: [Zjoi2005]Swamp 沼泽鳄鱼(矩阵快速幂)

程序员文章站 2022-05-25 19:55:40
题意 "题目链接" Sol 不难发现吃人鱼的运动每$12s$一个周期 所以暴力建12个矩阵,放在一起快速幂即可 最后余下的部分暴力乘 cpp include using namespace std; const int MAXN = 52, mod = 10000; inline int read( ......

题意

题目链接

sol

不难发现吃人鱼的运动每\(12s\)一个周期

所以暴力建12个矩阵,放在一起快速幂即可

最后余下的部分暴力乘

#include<bits/stdc++.h>
using namespace std;
const int maxn = 52, mod = 10000;
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}
int n, m, s, t, k, mp[maxn][maxn], pos[maxn];
int add(int x, int y) {
    if(x + y < 0) return x + y + mod;
    return x + y >= mod ? x + y - mod : x + y;
}
int add2(int &x, int y) {
    if(x + y < 0) x = x + y + mod;
    else x = (x + y >= mod ? x + y - mod : x + y);
}
int mul(int x, int y) {
    return 1ll * x * y % mod;
}
struct ma {
    int m[maxn][maxn];
    ma() {
        memset(m, 0, sizeof(m));
    }
    ma operator * (const ma &rhs) const {
        ma ans;
        for(int k = 1; k <= n; k++)
            for(int i = 1; i <= n; i++)
                for(int j = 1; j <= n; j++) 
                    add2(ans.m[i][j], mul(m[i][k], rhs.m[k][j]));
        return ans;
    }
}a[15], bg;
ma matrixfp(ma a, int p) {
    ma base;
    for(int i = 1; i <= n; i++) base.m[i][i] = 1;
    while(p) {
        if(p & 1) base = base * a;
        a = a * a; p >>= 1;
    }
    return base;
}
int main() {
    n = read(); m = read(); s = read() + 1; t = read() + 1; k = read();
    for(int i = 1; i <= m; i++) {int x = read() + 1, y = read() + 1; bg.m[x][y]++; bg.m[y][x]++;}
    int fn = read();
    for(int i = 1; i <= 12; i++) a[i] = bg;
    for(int i = 1; i <= fn; i++) {
        int num = read();
        for(int j = 1; j <= num; j++) pos[j] = read() + 1;
        for(int j = 1; j <= 12; j++)
            for(int k = 1; k <= n; k++)
                a[j].m[k][pos[j % num + 1]] = 0;
    }
    ma res = a[1];
    //for(int i = 1; i <= n; i++) res.m[i][i] = 1;
    for(int i = 2; i <= 12; i++) res = res * a[i];
    res = matrixfp(res, k / 12);
    for(int i = 1; i <= k % 12; i++) res = res * a[i];
    printf("%d\n", res.m[s][t]);
    return 0;
}
/*
6 8 1 5 333
0 2
2 1
1 0
0 5
5 1
1 4
4 3
3 5
3
3 0 5 1
2 1 2
4 1 2 3 4 
*/