zoukankan      html  css  js  c++  java
  • Codeforces 514E Darth Vader and Tree 矩阵快速幂

    Darth Vader and Tree

    感觉是个很裸的矩阵快速幂, 搞个100 × 100 的矩阵, 直接转移就好啦。

    #include<bits/stdc++.h>
    #define LL 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 ull unsigned long long
    
    using namespace std;
    
    const int N = 1e5 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    const double PI = acos(-1);
    
    int n, x, c[101];
    
    struct Matrix {
        int a[101][101];
        Matrix() {
            memset(a, 0, sizeof(a));
        }
        void init() {
            for(int i = 0; i < 101; i++)
                a[i][i] = 1;
        }
        Matrix operator * (const Matrix &B) const {
            Matrix C;
            for(int i = 0; i < 101; i++)
                for(int j = 0; j < 101; j++)
                    for(int k = 0; k < 101; k++)
                        C.a[i][j] = (C.a[i][j] + 1ll * a[i][k] * B.a[k][j]) % mod;
            return C;
        }
        Matrix operator ^ (int b) {
            Matrix C; C.init();
            Matrix A = (*this);
            while(b) {
                if(b & 1) C = C * A;
                A = A * A; b >>= 1;
            }
            return C;
        }
    } M;
    
    int main() {
        scanf("%d%d", &n, &x);
        for(int i = 1; i <= n; i++) {
            int v; scanf("%d", &v);
            c[v]++;
        }
        for(int j = 0; j < 100; j++) M.a[0][j] = c[j + 1]; M.a[0][100] = 1;
        for(int i = 1; i < 100; i++) M.a[i][i - 1] = 1; M.a[100][100] = 1;
        Matrix mat = M ^ (x);
        printf("%d
    ", (mat.a[0][0] + mat.a[0][100]) % mod);
        return 0;
     }
    
    /*
    */
  • 相关阅读:
    pandas去重方法
    原生表单组件
    html表单
    html表格基本标签
    文档和网站架构
    文本格式
    【Leetcode链表】奇偶链表(328)
    【Leetcode链表】移除链表元素(203)
    【Leetcode链表】旋转链表(61)
    【Leetcode链表】反转链表 II(92)
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10456745.html
Copyright © 2011-2022 走看看