JZOJ 1036. [lost] SCOI2009

topic

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.

 

analysis

 

  • First, if our view has only words of 0 and 1, T power of this matrix is ​​the answer
  • why
  • Set f [k] [i] [j] is the number of the program went i, j at time k is apparently f [k] [i] [j] = Σ1-nf [ka [i] [j]] [i ] [j] a [i] [j] do not both 1 so simplification
  •  f [k] [i] [j] = Σ1-nf [k-1] [i] [j] is not the same as on the original equation and it
  • Then for this question
  • We split point
  • It is split into a plurality of points of Matrix Fast Power 

 

Code

 

 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 const int maxn=90,p=2009; 
 5 int tmp[maxn][maxn],bb[maxn][maxn], ans[maxn][maxn];
 6 int id,n,t;
 7 void mul_mat(int a[][maxn],int b[][maxn])
 8 {
 9     memset(tmp,0,sizeof(tmp));
10     for (int i=0;i<id;i++)
11       for (int j=0;j<id;j++)
12         for (int k=0;k<id;k++)
13             tmp[i][j]=(tmp[i][j]+a[i][k]*b[k][j]%p)%p;
14     memcpy(a,tmp,sizeof(tmp));
15 }
16 void ksm()
17 {
18     for (int i=0;i<id;i++)
19        ans[i][i]=1;
20     int tt=t;
21     while (tt)
22     {
23         if (tt&1) mul_mat(ans,bb);
24         mul_mat(bb,bb);
25         tt>>=1;
26     }
27 }
28 int main ()
29 {
30     char c;
31     cin>>n>>t;
32     id=n*9;
33     for (int i=0;i<n;i++)
34     {
35         for (int j=0;j<8;j++)
36             bb[i*9+j][i*9+j+1]=1;
37         for (int j=0;j<n;j++)
38         {
39             cin>>c;
40             c-='0';
41             if (c) bb[i*9+c-1][j*9]=1;
42         }
43     }
44     ksm();
45     cout << years [ 0 ] [(n- 1 ) * 9 ];
46 }

 

Guess you like

Origin www.cnblogs.com/zjzjzj/p/11330416.html