P3758 [TJOI2017]可乐

P3758 [TJOI2017]可乐


这个题是利用了邻接矩阵的性质

即:对于一个邻接矩阵\(E\),表示从\(E[i][j]\)\(i\)\(j\)路径长度为1的方案数是多少。那么\(E^k[i][j]\)表示从\(i\)\(j\)路径长度为\(K\)

对这个题

对于走到其他城市,就可以直接使用邻接矩阵想乘

而对于原地不动,我们可以理解成向当前的城市添加一个自环就行了

然后对于自爆,我们就是前一秒的方案数都存下来,我们就可以设一个零号城市,所有城市都可以到他,然后这个城市无法到达其他城市

然后就可以愉快的快速幂了

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
const int maxn=110;
const int RQY=2017;
struct node
{
    int n,m;
    int map[maxn][maxn];
    node (int n=0,int m=0)
    {
        memset(map,0,sizeof(map));
    }
    node operator * (const node &a)const 
    {
        node res;
        res.n=n,res.m=a.m;
        for(int i=0;i<=n;i++)
            for(int j=0;j<=res.m;j++)
                for(int k=0;k<=m;k++)
                    res.map[i][j]=(res.map[i][j]+map[i][k]*a.map[k][j])%RQY;
        return res;
    }
};
node A,C;
node kasumi(node b,int t)
{
    node Res=b;t--;
    while(t)
    {
        if(t&1)
            Res=Res*b;
        t>>=1;
        b=b*b;
    }
    return Res;
}
int main()
{
    int n,m,T;
    scanf("%d%d",&n,&m);
    A.n=n;A.m=n;
    for(int i=0;i<=n;i++)   A.map[i][i]=A.map[i][0]=1;
    int a,b;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d",&a,&b);
        A.map[a][b]++;
        A.map[b][a]++;
    }
    scanf("%d",&T);
    C=kasumi(A,T);
    int tot=0;
    for(int i=0;i<=C.m;i++)
        tot=(tot+C.map[1][i])%RQY;
    printf("%d",tot);
}

猜你喜欢

转载自www.cnblogs.com/Lance1ot/p/9612519.html
今日推荐