Hdu 5411 CRB and Puzzle 2015 multi-school Joint Training Tournament # # Fast Matrix Power

Source: Internet
Author: User

CRB and Puzzle Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): Accepted submission (s): 35


Problem Description CRB is now playing Jigsaw Puzzle.
There is N kinds of pieces with infinite supply.
He can assemble one piece to the right side of the previously assembled one.
For each kind of pieces, only restricted kinds can is assembled with.
How many different patterns he can assemble with at the most M pieces? (Patterns P and Q are considered different if their lengths is different or there exists an integer j such that J -th piece of P is different from corresponding piece of Q.)

Input There is multiple test cases. The first line of input contains an integer T, indicating the number of the test cases. For each test case:
The first line contains the integers N, M denoting the number of kinds of pieces and the maximum number of moves.
Then N lines follow. I-th line is described as following format.
K A1 A2 ... ak
Here are the number of kinds which can be assembled to the right of the i-th kind. Next k integers represent each of the them.
1≤t≤20
1≤n≤50
1≤m≤105
0≤k≤n
1≤A1 < A2 < ... < ak≤n


Output a single integer-number of different patterns modulo 2015.
Sample Input

1 3 2 1 2 1 3 0
Sample Output
6 Hint Possible patterns are∅, 1, 2, 3, 1→2, 2→3
Author KUT (DPRK)
Source multi-university Training Contest 10

Set up a transfer matrix and add a node to indicate the condition of ans.

===========================

According to someone's comments, how to record the number of ans. We now assume that the following equation A is a transfer matrix with three states

If the shift is linear, then each shift, ans + = dp[1]+dp[2]+dp[3], so that the current length of the answer is recorded.

How to achieve the transfer of ANS in the matrix.

The state of the ANS is added to the matrix, because at first, the type of length 0 has one, which constructs the following matrix with the state A to represent the transfer matrix.

a[1,1,1,1] = A[dp[1],dp[2],dp[4],ans] (1 of the length of the case, any number can be put, so all 1)

The next step is how to construct a so that each time you multiply a, the ANS will update:

as follows A =

x x x 0 dp[1]

x x x 0 * dp[2]

x x x 0 dp[3]

1 1 1 1 ans

So we know that each multiply A,DP value will change, the DP value represents the length of the current length, the number of cases ending with I.

So in this multiplication ans = dp[1] + dp[2] + dp[3] + ans, ans not only recorded the last value, but also added the last

Dp[1],dp[2],dp[3], in other words, ans is the sum of the different lengths of the case.

Then it can be understood by using matrix acceleration.



#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace
Std
int mod = 2015;

const int MAXN = 52;
    struct node{int MAT[MAXN][MAXN];
    Node () {memset (mat,0,sizeof (MAT));

}
};
    Node operator* (node A,node b) {node C;
                for (int i = 0;i < MAXN, i++) for (int j = 0;j < Maxn; j + +) for (int k = 0;k < MAXN; k++)
    C.MAT[I][J] + = a.mat[i][k]*b.mat[k][j];
    for (int i = 0;i < MAXN; i++) for (int j = 0;j < Maxn; j + +) C.mat[i][j]%= mod;
return C;
    } int main () {int t,n,m;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &n,&m);
        Node A,b,c;
            for (int i = 0;i < n; i++) {int k;
            scanf ("%d", &k);
                while (k--) {int u;
                scanf ("%d", &u);
                u--;
            A.mat[u][i] = 1; }} for (int i = 0;i <=N
        i++) A.mat[n][i] = 1;
        for (int i = 0;i <= n; i++) b.mat[i][i] = 1;

        int ans = 0;
            while (m) {if (m&1) b = a*b;
            A = A*a;
        m/=2;
        } for (int i = 0;i <= n; i++) ans + = b.mat[n][i];
        Ans%= MoD;
    printf ("%d\n", ans);
} return 0;












 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.