zoukankan      html  css  js  c++  java
  • HDU

    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
    ", ans);
        }
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    天梯赛5-12 愿天下有情人都是失散多年的兄妹 【dfs】
    poj2718 Smallest Difference【贪心】
    HDU problem 5635 LCP Array【思维】
    codeforces 782C Andryusha and Colored Balloons【构造】
    HDU 4278 Faulty Odometer【进制转换】
    codeforces B. The Meeting Place Cannot Be Changed【二分】
    POJ 3264 Balanced Lineup 【线段树】
    HDU 1850
    CodeForces-714C
    HDU Problem 1247 Hat's Words 【字典树】
  • 原文地址:https://www.cnblogs.com/CJLHY/p/11156609.html
Copyright © 2011-2022 走看看