2017 Wuhan University Programming Contest (Online Round) E. Lost in WHU(矩阵快速幂)

来源:互联网 发布:安捷伦66319d编程手册 编辑:程序博客网 时间:2024/05/22 08:04
Input file: standard inputOutput file: standard output Time limit: 1 secondMemory limit: 512 mebibytes

As one of the most beautiful campus in China, Wuhan University is around several hills, so the road is complex and visitors always lose themselves. Give a undirected graph of WHU of NN points and a deadline, then you should count the number of plan to reach the destination by deadline (the starting point is 1 and the ending point is NN).

Input

First line contains the number of points NN (N\le 100N100) and the number of edges MM (M\le N(N-1)/2MN(N1)/2).

The ii-th line of next MM lines contains two numbers u_iui and v_ivi, which respents an edge (u_i, v_i)(ui,vi).

The last line contains the deadline TT(T\le 10^9T109).

Output

The number of plan to reach the destination by deadline module 10^9+7109+7.

Examples

Input 1

4 51 32 33 41 21 48

Output 1

170

题目链接https://oj.ejq.me/problem/26

给出可以走的边,以及走的步数,问在这个步数内有多少种方案从1到达n,设置一下到达n后不能继续走,然后直接用矩阵快速幂即可

#include <iostream>#include <iomanip>#include<stdio.h>#include<string.h>#include<stack>#include<stdlib.h>#include<queue>#include<map>#include<math.h>#include<algorithm>#include<vector>#define mem(a,b) memset(a,b,sizeof(a))#define N 105#define inf 1000000007#define ll long longusing namespace std;struct Matrix{    ll m[N][N];};int n;Matrix res;//单位矩阵Matrix mul(Matrix a,Matrix b)//矩阵相乘{    Matrix c;mem(c.m,0);    for(int i=1;i<=n;i++)       for(int j=1;j<=n;j++)        for(int k=1;k<=n;k++)        c.m[i][j]=(c.m[i][j]+a.m[i][k]*b.m[k][j])%inf;    return c;}Matrix fastm(Matrix a,int x)//矩阵快速幂{    while(x)    {        if(x&1)            res=mul(res,a);        x>>=1;        a=mul(a,a);    }    return res;}int main(){    int m,x,y,t;    while(cin>>n>>m)    {        Matrix a,b;        for(int i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)                res.m[i][j]=(i==j);        }        for(int i=1;i<=m;i++)        {            cin>>x>>y;            a.m[x][y]=a.m[y][x]=1;            if(y==n)a.m[y][x]=0;//到达n后不能再走            if(x==n)a.m[x][y]=0;//到达n后不能再走        }        a.m[n][n]=1;//计算<=k步的路径总数,如果为0,则计算=k时的路径数        cin>>t;        b=fastm(a,t);        cout<<b.m[1][n]<<endl;    }}



0 0