acm-K steps

来源:互联网 发布:淘宝店铺收藏在哪里 编辑:程序博客网 时间:2024/04/26 13:26

K steps

时间限制:2000 ms  内存限制:65535 KB
难度:4
描述
Here are n beautiful towns and m roads(directional edge). yjxwants to visit these towns for relaxation when hesuddenly 
got a question. He wants to know the number of schemes to walk from town A to town B in exactly ksteps.A road can be 
visited more then once. It takes exactly one step to walk fromone town to another if they are directly connected by a road.
yjx is very entangled with this matter, please help him.
输入
First line is a number T, the number of the cases.
Each case is as follows:
First line includes four number: n, m, k, l which means n(1 <= n<= 100)towns, m(1 <= m<=1000)roads,k(1<=k<=1000)steps,
and l (1<=l<=1000) lines test data.
Then there are m lines, and each line is made up of two number u, v(u != v, 1<= u,v <= n) which means one road from u tov.
Then l lines test data, and each line is made up of two number p, q(so you must help yjx to know the number of the scheme that justwalk k steps from town p to town q).
hint: maybe there are more than one road from u to v .
输出
For each test data, output the number of the scheme(the number isbig, so you must make the number mod 1991).
样例输入
22 2 1 21 22 11 22 13 2 2 11 22 31 3
样例输出
111
上传者
521521


代码:

#include
#include
#define max 101
#define len 1991
using namespace std;
typedef struct numb
{
 long long a[max][max]; 
}MT;

MT ans;
int n,m,k,l;
void mult(numb &x,numb y,numb z)
{
 memset(x.a,0,sizeof(x.a));
 for(int i=0;i
  for(int j=0;j
  {
   x.a[i][j]=0;
   for(ints=0;s
   {
    x.a[i][j]+=y.a[i][s]*z.a[s][j];
    
   x.a[i][j]%=len;
  }
}

int main()
{
 int t;
 int u,v;
 int p,q;
 scanf("%d",&t);
 while(t--)
 {
  scanf("%d%d%d%d",&n,&m,&k,&l);
  memset(ans.a,0,sizeof(ans.a));
  
  for(int i=0;i
  {
   scanf("%d%d",&u,&v);
   ans.a[u-1][v-1]+=1;
  
  MT res;
  memset(res.a,0,sizeof(res.a));
  for(int i=0;i
   res.a[i][i]=1;
  while(k)
  {
   if(k&1)
    mult(res,res,ans);
   k>>=1;
   mult(ans,ans,ans);  
  }

  for(int i=0;i
  {
   scanf("%d%d",&p,&q);
   printf("%d\n",res.a[p-1][q-1]);  
  }
 }
 return 0;
}