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\n", (mat.a[0][0] + mat.a[0][100]) % mod);
    return 0;
 }

/*
*/

 

posted @ 2019-03-01 15:37  NotNight  阅读(159)  评论(0编辑  收藏  举报