[HAOI2015] 数字串拆分

题目链接

矩阵加速裸题。注意G(x+1)=G(x)*A。

// luogu-judger-enable-o2
#include <bits/stdc++.h>
#define ll long long 
using namespace std;

const int N=5e2+5;
const int inf=0x3f3f3f3f;
const int mod=998244353;

int n,m;
char str[N];

struct Node {
    int a[6][6];
    inline int*operator[](const int&d) {return a[d];}
    inline const int*operator[](const int&d) const{return a[d];}
    inline Node operator*(const Node&b) const{
        Node c; 
        memset(&c,0,sizeof c);
        for(int i=1; i<=m; ++i) 
        for(int k=1; k<=m; ++k) if(a[i][k]) 
        for(int j=1; j<=m; ++j) (c[i][j]+=1LL*a[i][k]*b[k][j]%mod)%=mod;
        return c;
    }
    inline Node operator+(const Node&b) const{
        Node c; 
        memset(&c,0,sizeof c);
        for(int i=1; i<=m; ++i) 
        for(int j=1; j<=m; ++j) c[i][j]=(a[i][j]+b[i][j])%mod;
        return c;
    }
};
Node f[N],g[N][N],pw[N];

inline Node pow10(Node x) {
    Node t=x=x*x; x=x*x;
    return x*x*t;
}

int main() {
    scanf("%s%d",str+1,&m);
    n=strlen(str+1);
    for(int i=1; i<=m; ++i) pw[0][i][i]=1;
    for(int i=1; i<=m; ++i) pw[1][i][m]=1;
    for(int i=2; i<=m; ++i) pw[1][i][i-1]=1;
    for(int i=2; i<10; ++i) pw[i]=pw[1]*pw[i-1];
    for(int i=1; i<=n; ++i) {
        g[i][i-1]=pw[0];
        for(int j=i; j<=n; ++j) {
            g[i][j]=pow10(g[i][j-1])*pw[str[j]-'0'];
        }
    }
    f[0]=pw[0];
    for(int i=1; i<=n; ++i) 
    for(int j=i-1; ~j; --j) f[i]=f[i]+f[j]*g[j+1][i];
    int ans=0;
    for(int i=1; i<=m; ++i) (ans+=f[n][1][i])%=mod;
    printf("%d\n",ans); 
    return 0;
}

转载于:https://www.cnblogs.com/nosta/p/11040141.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值