ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

HDU - 5411 CRB and Puzzle 矩阵快速幂

2019-07-09 13:03:09  阅读:210  来源: 互联网

标签:HDU Matrix 5411 int Puzzle MN ++ const define


HDU - 5411

考虑直接dp会T, 用矩阵优化一下就好了。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);

using namespace std;

const int N = 50 + 2;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 2015;
const double eps = 1e-8;
const double PI = acos(-1);

template<class T, class S> inline void add(T &a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T &a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T &a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T &a, S b) {return a > b ? a = b, true : false;}

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int MN, n, m;

struct Matrix {
    int a[N][N];
    Matrix() {
        for(int i = 0; i < MN; i++) {
            for(int j = 0; j < MN; j++) {
                a[i][j] = 0;
            }
        }
    }
    void init() {
        for(int i = 0; i < MN; i++) {
            a[i][i] = 1;
        }
    }
    Matrix operator * (const Matrix &B) {
        Matrix C;
        for(int i = 0; i < MN; i++) {
            for(int j = 0; j < MN; j++) {
                for(int k = 0; k < MN; k++) {
                    C.a[i][j] = (C.a[i][j] + a[i][k] * B.a[k][j]) % mod;
                }
            }
        }
        return C;
    }
    Matrix operator ^ (int b) {
        Matrix A = (*this);
        Matrix C; C.init();
        while(b) {
            if(b & 1) C = C * A;
            A = A * A; b >>= 1;
        }
        return C;
    }
} mat;

int main() {
    int T; scanf("%d", &T);
    while(T--) {
        scanf("%d%d", &n, &m);
        MN = n + 1;
        for(int i = 0; i < MN; i++) {
            for(int j = 0; j < MN; j++) {
                mat.a[i][j] = (i == MN - 1) ? 1 : 0;
            }
        }
        for(int i = 0; i < n; i++) {
            int k, x; scanf("%d", &k);
            while(k--) {
                scanf("%d", &x);
                mat.a[i][x - 1]++;
            }
        }
        Matrix ret = mat ^ m;
        int ans = 0;
        for(int j = 0; j < MN; j++) {
            add(ans, ret.a[MN - 1][j]);
        }
        printf("%d\n", ans);
    }
    return 0;
}

/*
*/

 

标签:HDU,Matrix,5411,int,Puzzle,MN,++,const,define
来源: https://www.cnblogs.com/CJLHY/p/11156609.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有