--- --- restore content begins
Title Description
Input Format
Output Format
Sample
Data range and tips
Ideas:
This problem can be said to be a problem theorems, in a matrix, a memory number i to j path matrix multiplied by itself k times, A [i] [j] denotes the number of walking steps k from i to j path. Let's prove it, in fact, a closer look to understand matrix multiplication, c [i] [j] + = a [i] [k] * b [k] [j], written on a clear, and enumerate an intermediate point, multiplication principle, the two-step i to j number of paths equal to the number k i to k by the path step by step to the path j, and then with a different intermediate point adder principle, it must be correct and complete, every i can enumerate all points to, and all can point to j , k step is clearly the same . This question and then find not been resolved, because his right side may be 1-9, if direct deposit into the matrix, then keep right side is clearly undesirable, it said there are several paths to the right side, as if deposit 1 It became one step to get to. Correct answer is a point to split into nine points, can be considered to engage in a 9-band, then even the edges, the specific implementation is not good narrative, something like this
for(int i=1;i<d;i++) a.a[x*9+i][x*9+i+1]=1; a.a[x*9+d][y*9+1]=1;
Then we can ensure that the right side is the time to properly handle k.
#include<cstdio> #include<iostream> #define ll long long using namespace std; const int mod=2009; struct node{ll a[102][102];}a,ans; ll n,t; void cheng(node a,node b,node &c) { for(int i=1;i<=n*9;i++) for(int j=1;j<=n*9;j++) { c.a[i][j]=0; for(int l=1;l<=n*9;l++) c.a[i][j]=(c.a[i][j]+(a.a[i][l]*b.a[l][j]))%mod; } } void qpow(node a,ll b) { while(b) { if(b&1) cheng(ans,a,ans); cheng(a,a,a); b>>=1; } } void add(int x,int y,int d) { if(d==0) return ; for(int i=1;i<d;i++) a.a[x*9+i][x*9+i+1]=1; // cout<<x*9+i<<" "<<x*9+i+1<<" "<<a.a[x*9+i][x*9+i+1]<<endl; a.a[x*9+d][y*9+1]=1; } int main() { int d; cin>>n>>t; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { scanf("%1d",&d); //cout<<d<<" "; add(i,j,d); } ans=a; qpow(a,t-1); // for(int i=1;i<=(n-1)*9+1;i++) // { // for(int j=1;j<=(n-1)*9+1;j++) // cout<<ans.a[i][j]<<" "; // cout<<endl; // } cout<<ans.a[0*9+1][(n-1)*9+1]; }