首页 > 其他 > 详细

【51nod】2590 持续讨伐

时间:2019-06-19 14:28:56      阅读:104      评论:0      收藏:0      [点我收藏+]

【51nod】2590 持续讨伐

挣扎着卡了卡唱过了

\(dp[i][j]\)为到第\(i\)位,和第\(i\)位相连的部分长度\(x^{j}\)乘上之前部分所有方案\(x^{K}\)总和

转移用二项式定理展开即可,若这位不选,可以有\(dp[i + 1][j] = dp[i][K]\)

矩阵乘法优化一下,卡常用预处理出2的几次幂的矩阵的答案

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 2005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 +c - '0';
    c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    out(x / 10);
    }
    putchar('0' + x % 10);
}
const int MOD = 998244353;
int N,M,K;
int C[15][15];
bool vis[55];
int inc(int a,int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}
int mul(int a,int b) {
    return 1LL * a * b % MOD;
}
void update(int &x,int y) {
    x = inc(x,y);
}
struct Matrix {
    int f[9][9];
    Matrix() {memset(f,0,sizeof(f));}
    friend Matrix operator * (const Matrix &a,const Matrix &b) {
    Matrix c;
    for(int i = 0 ; i <= K ; ++i) {
        for(int j = 0 ; j <= K ; ++j) {
        for(int h = 0 ; h <= K ; ++h) {
            update(c.f[i][j],mul(a.f[i][h],b.f[h][j]));
        }
        }
    }
    return c;
    }
    void unit() {
    for(int i = 0 ; i <= K ; ++i) f[i][i] = 1;
    }
    friend Matrix fpow(Matrix a,int c) {
    Matrix res,t = a;res.unit();
    while(c) {
        if(c & 1) res = res * t;
        t = t * t;
        c >>= 1;
    }
    return res;
    }
}A,B,ans,P[35];
void Solve() {
    read(N);read(M);read(K);
    for(int i = 0 ; i <= K ; ++i) {
    C[i][0] = 1;
    for(int j = 1 ; j <= i ; ++j) {
        C[i][j] = inc(C[i - 1][j - 1],C[i - 1][j]);
    }
    }
    for(int i = 0 ; i <= K ; ++i) {
    for(int j = 0 ; j <= i ; ++j) {
        update(A.f[j][i],C[i][j]);
    }
    }
    
    B = A;
    for(int i = 0 ; i <= K ; ++i) update(B.f[K][i],1);
    P[0] = B;
    for(int i = 1 ; i <= 30 ; ++i) P[i] = P[i - 1] * P[i - 1];
    int p = 1,t;
    ans.unit();
    for(int i = 1 ; i <= M ; ++i) {
    read(t);
    for(int j = 0 ; j <= 29 ; ++j) {
        if((t - p) >> j & 1) ans = ans * P[j];
    }
    p = t;
    ans = ans * A;++p;
    }
    if(p != N) {
    for(int j = 0 ; j <= 29 ; ++j) {
        if((N - p) >> j & 1) ans = ans * P[j];
    }
    }
    int res = 0;
    for(int i = 0 ; i <= K ; ++i) {
    update(res,ans.f[i][K]);
    }
    out(res);enter;
}
int main(){
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}

【51nod】2590 持续讨伐

原文:https://www.cnblogs.com/ivorysi/p/11050912.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!