zoukankan      html  css  js  c++  java
  • 2015 Multi-University Training Contest 10 hdu 5411 CRB and Puzzle

    CRB and Puzzle

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 301    Accepted Submission(s): 127

    Problem Description
    CRB is now playing Jigsaw Puzzle.
    There are 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 be assembled with.
    How many different patterns he can assemble with at most M pieces? (Two patterns P and Q are considered different if their lengths are different or there exists an integer j such that j-th piece of P is different from corresponding piece of Q.)

    Input
    There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
    The first line contains two 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 a_1 a_2 ... a_k$
    Here k is the number of kinds which can be assembled to the right of the i-th kind. Next k integers represent each of them.
    $1 leq T leq 20$
    $1 leq N leq 50$
    $1 leq M leq 10^5$
    $0 leq k leq N$
    $1 leq a1 < a2 < … < ak leq N$

    Output
    For each test case, 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)

    解题:矩阵快速幂+动态规划

    $$egin{bmatrix} ret & dp1 & dp2 & dp3 \ end{bmatrix} imes egin{bmatrix} 1 & 0 & 0 & 0 \ 1 & 0 & 0 & 0 \ 1 & 1 & 0 & 0 \ 1 & 0 & 1 & 0 \ end{bmatrix} $$

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 const int maxn = 60;
     4 const int mod = 2015;
     5 int n,m;
     6 struct Matrix {
     7     int m[maxn][maxn];
     8     void init() {
     9         memset(m,0,sizeof m);
    10     }
    11     void setOne() {
    12         init();
    13         for(int i = 0; i < maxn; ++i) m[i][i] = 1;
    14     }
    15     Matrix() {
    16         init();
    17     }
    18     Matrix operator*(const Matrix &t)const {
    19         Matrix ret;
    20         for(int k = 0; k <= n; ++k) {
    21             for(int i = 0; i <= n; ++i)
    22                 for(int j = 0; j <= n; ++j)
    23                     ret.m[i][j] = (ret.m[i][j] + m[i][k]*t.m[k][j])%mod;
    24         }
    25         return ret;
    26     }
    27 };
    28 Matrix a,b,c;
    29 int main() {
    30     int kase;
    31     scanf("%d",&kase);
    32     while(kase--) {
    33         scanf("%d%d",&n,&m);
    34         b.init();
    35         a.init();
    36         c.setOne();
    37         for(int i = 1,t,k; i <= n; ++i) {
    38             scanf("%d",&t);
    39             while(t--) {
    40                 scanf("%d",&k);
    41                 b.m[k][i] = 1;
    42             }
    43         }
    44         for(int i = 0; i <= n; ++i) {
    45             a.m[0][i] = 1;
    46             b.m[i][0] = 1;
    47         }
    48         a.m[0][0] = 0;
    49         while(m) {
    50             if(m&1) c = c*b;
    51             m >>= 1;
    52             b = b*b;
    53         }
    54         a = a*c;
    55         printf("%d
    ",(a.m[0][0]+1)%mod);
    56     }
    57     return 0;
    58 }
    View Code
  • 相关阅读:
    Java实现荷兰国旗问题
    Java实现荷兰国旗问题
    QTcpSocket 对连接服务器中断的不同情况进行判定(六种情况,其中一种使用IsNetworkAlive API方法)
    怀旧老游戏
    我的第一个Chrome插件:天气预报应用
    ddd
    Android 反编译工具
    ASP.NET MVC局部视图
    jQuery类库的设计
    jquery+html三级联动下拉框
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4749113.html
Copyright © 2011-2022 走看看