[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;
}
posted @ 2019-06-17 15:50  nosta  阅读(118)  评论(0编辑  收藏  举报