BZOJ 1898 构造矩阵+矩阵快速幂

思路:
T的最小公倍数是12
那么12以内暴力 整除12 的部分用矩阵快速幂

//By SiriusRen
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n,m,st,ed,k,nfish,T,p[5],can[13][55];
struct Matrix{
    int a[55][55];
    void init(){memset(a,0,sizeof(a));}
    void Change(){for(int i=0;i<n;i++)a[i][i]=1;}
    friend Matrix operator*(Matrix a,Matrix b){
        Matrix c;c.init();
        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]+=a.a[i][k]*b.a[k][j];
                c.a[i][j]%=10000;
            }
        return c;
    }
    friend Matrix operator^(Matrix a,int b){
        Matrix c;c.init();c.Change();
        for(;b;b>>=1,a=a*a)if(b&1)c=c*a;
        return c;
    }
}begin[13];
struct Path{int from,to;}path[2555];
int main(){
    scanf("%d%d%d%d%d",&n,&m,&st,&ed,&k);
    for(int i=1;i<=m;i++)scanf("%d%d",&path[i].from,&path[i].to); 
    scanf("%d",&nfish);
    while(nfish--){
        scanf("%d",&T);
        for(int i=0;i<T;i++)scanf("%d",&p[i]);
        for(int i=0;i<12;i++)can[i][p[i%T]]=1;
    }
    for(int i=0;i<n;i++)can[12][i]=can[0][i];
    begin[0].Change();
    for(int i=1;i<=12;i++){
        for(int j=1;j<=m;j++){
            if(!can[i-1][path[j].from]&&!can[i][path[j].to])
                begin[i].a[path[j].from][path[j].to]++;
            if(!can[i-1][path[j].to]&&!can[i][path[j].from])
                begin[i].a[path[j].to][path[j].from]++;
        }
        begin[0]=begin[0]*begin[i];
    }
    begin[0]=begin[0]^(k/12);
    for(int i=1;i<=k%12;i++)begin[0]=begin[0]*begin[i];
    printf("%d\n",begin[0].a[st][ed]);
}

这里写图片描述

posted @ 2017-01-11 11:28  SiriusRen  阅读(166)  评论(0编辑  收藏  举报