JZ High School OJ 1036. [SCOI2009] lost

Description

windy lost. In the drawing there
The N nodes have to FIG, Windy from node 0, node must arrive exactly at the time T N-1.
Now given the directed graph, you can tell how many different paths windy total it?
Note: windy can not stay in a node, and directed by a strict time edge for a given period of time.
 

Input

The first line contains two integers, NT.
Then there are N rows, each row of a string of length N.
The i-th row and j as '0' from node i to node j no edges.
'1' to '9' represents from node i to node j takes time.

Output

Output an integer number of possible paths, this number may be large, the output simply divide this number by the remainder of 2009.
 

Sample Input

2 2
11
00

Sample Output

1
 

Data Constraint

 
 

Hint

100% data satisfies 2 <= N <= 10; 1 <= T <= 1000000000.
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int mod=2009;
 4 int n,t;
 5 struct pp{
 6     int m[115][115];
 7 }a;
 8 int c;
 9 inline pp mul(pp b,pp c)
10 {
11     pp t;
12     for(int i=1;i<=10*n;i++)
13     for(int j=1;j<=10*n;j++){
14         t.m[i][j]=0;
15         for(int k=1;k<=10*n;k++){
16             t.m[i][j]=(t.m[i][j]+(b.m[i][k]*c.m[k][j])%mod)%mod;
17         }
18     }
19     return t;
20 }
21 inline int fast(int x)
22 {
23     pp ans=a;
24     pp base=a;
25     while(x)
26     {
27         if(x&1)ans=mul(ans,base);
28         base=mul(base,base);
29         x=x>>1;
30     }
31     return ans.m[1][n];
32 }
33 int main()
34 {
35     cin>>n>>t;
36     for(int i=1;i<=n;i++)
37     {
38         for(int j=1;j<=8;j++)
39         a.m[i+j*n][i+(j-1)*n]=1;
40         for(int j=1;j<=n;j++)
41         {
42             scanf("%1d",&c);
43             a.m[i][j+n*(c-1)]=1;
44         }
45     }
46     cout<<fast(t-1);  
47     return 0;
48 }

Guess you like

Origin www.cnblogs.com/anbujingying/p/11329491.html