SCOI2009 lost problem solution

--- --- restore content begins

Title Description

Windy lost. In the drawing there The digraph NN N nodes, Windy from node 00 starting 0, he must just TT reaches the node at time T N-1N-1 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 Format

The first line contains two integers, N, the TN, T N , T;
Then there NN N rows, each row of a length NN N strings. The first II I line JJ J as 0expressed from a node II I to node JJ J no sides, is 1to 9represent the node II I to node JJ J takes time.

Output Format

Contains an integer number of possible paths, this number may be large, just the output divided by 20,092,009 2 0 0 remainder 9.

Sample

Sample input 1

2 2
11
00

Sample output 1

1

Sample Description 1

→ 0 → 0 10 \ 0 to \ to 1 0 0 1

Sample input 2

5 30
12045
07105
47805
12024
12345

Sample output 2

852

Data range and tips

For 30% of the data, to meet 2≤N≤5,1≤T≤30
for 100% of the data, to meet 2≤N≤10,1≤T≤10 ^. 9 ...... . 9 .

   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];
    
}
Ugly death does not override the operator code

 

 

Guess you like

Origin www.cnblogs.com/starsing/p/11204738.html