Descriptionwindy was lost in the direction of the map. The graph has N nodes, and windy from node 0, he must arrive at the node N-1 at the T moment. Now given the graph, can you tell windy how many different paths there are in total? Note: Windy cannot stay on a node and is strictly at a given time by the time it has a forward edge. The first line of input contains two integers, N T. Next there are n rows, one string of length n for each line. Line I, J, is listed as ' 0 ' to indicate that there is no edge from node I to node J. ' 1 ' to ' 9 ' represents the time it takes to get from node I to node J. Output contains an integer, the number of possible paths, which may be large, just the remainder of the number divided by 2009. Sample Input"Input Sample One"
2 2
11
00
"Input Sample Two"
5 30
12045
07105
47805
12024
12345
Sample Output"Output Example One"
1
"Sample Interpretation One"
0->0->1
"Output Example II"
852
HINT
30% of the data, meet 2 <= N <= 5; 1 <= T <= 30.
100% of the data, meet 2 <= N <=, 1 <= T <= 1000000000.
Idea: Matrix Fast power should be the first can think of, but directly to a length of 9 of the side to split into 9 points, the worst case there is a 9*9*9 point about 700 more points, the time complexity is n*n*n*log (t) in front of the obvious will explode, but can do so, a point split into 9 points, 9 points into a chain, so it can be messed up, if a point x to this point y has a length of K edge as long as the x is connected to the front of Y K-1 point on the line (because a side is reduced by one side)
1#include <cstdio>2#include <iostream>3#include <cstring>4 #defineN 905 #defineMOD 20096 using namespacestd;7 Charch[ -][ -];8 structMat9 {Ten Long Longm[n+1][n+1]; OneMat () {memset (M,0,sizeof(M));} A }; -Matoperator*(Mat A,mat b) - { the mat ans; - for(intI=1; i<=n;i++) - { - for(intj=1; j<=n;j++) + { - for(intk=1; k<=n;k++) + { AANS.M[I][J] = (Ans.m[i][j] + a.m[i][k] * b.m[k][j])%MOD; at } - } - } - returnans; - } -Mat Pow (Mat A,Long LongN) in { - MAT ret; to for(intI=1; i<=n;i++) ret.m[i][i]=1; + for(; n;n>>=1) - { the if(n&1) ret = (ret *a); *A = (A *a); $ }Panax Notoginseng returnret; - } the intMain () + { A intn,t; the Mat A; +scanf"%d%d",&n,&t); - for(intI=1; i<=n;i++) $ { $scanf"%s", ch[i]+1); - } - for(intI=1; i<=n;i++) the { - for(intj=1; j<=8; j + +)Wuyi { thea.m[(I-1)*9+j][(I-1)*9+j+1]=1; - } Wu } - for(intI=1; i<=n;i++) About { $ for(intj=1; j<=n;j++) - { - intU = ch[i][j]-'0'; - if(u!=0) A { +a.m[(I-1)*9+9[J-1)*9+(9-u+1)]=1; the } - } $ } theA =Pow (a,t); theprintf"%lld\n", a.m[9[N-1)*9+9]); the return 0; the}
Bzoj 1297: [SCOI2009] lost [matrix fast Power]