BZOJ4887 Tjoi2017可乐(动态规划+矩阵快速幂)

  设f[i][j]为第i天到达j号城市的方案数,转移显然,答案即为每天在每个点的方案数之和。矩乘一发即可。

#include<iostream> 
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 33
#define M 110
#define P 2017
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')&&(c<'0'||c>'9')) c=getchar();return c;}
int gcd(int n,int m){return m==0?n:gcd(m,n%m);}
int read()
{
    int x=0,f=1;char c=getchar();
    while (c<'0'||c>'9') {if (c=='-') f=-1;c=getchar();}
    while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar();
    return x*f;
}
int n,m,k;
struct matrix
{
    int n,a[N][N];
    matrix operator *(const matrix&b) const
    {
        matrix c;c.n=n;memset(c.a,0,sizeof(c.a));
        for (int i=0;i<n;i++)
            for (int j=0;j<b.n;j++)
                for (int k=0;k<b.n;k++)
                c.a[i][j]=(c.a[i][j]+a[i][k]*b.a[k][j])%P;
        return c;
    }
}f,v;
int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj4887.in","r",stdin);
    freopen("bzoj4887.out","w",stdout);
    const char LL[]="%I64d\n";
#else
    const char LL[]="%lld\n";
#endif
    n=read(),m=read();
    v.n=n+1;
    for (int i=1;i<=m;i++)
    {
        int x=read(),y=read();
        v.a[x][y]=v.a[y][x]=1;
    }
    for (int i=0;i<=n;i++) v.a[i][i]=v.a[i][0]=1;
    k=read()+1;
    f.n=1;f.a[0][1]=1;
    for (;k;k>>=1,v=v*v) if (k&1) f=f*v;
    cout<<f.a[0][0];
    return 0;
}

 

posted @ 2018-11-27 21:56  Gloid  阅读(188)  评论(0编辑  收藏  举报