题解:P14084 「CZOI-R7」敲击
矩阵优化还是挺优美的。
先来考虑能否构造出一个 countbit 为
考虑构造出的数字小于 countbit 为 countbit 为
其中
然后把上述内容全部套进矩阵优化里就行了。矩阵优化的实现细节见代码。
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define N 210
#define INF 0x3f3f3f3f
#define lowbit(x) (x&-x)
#define pii pair<int,int>
#define cpx complex<double>
#define poly vector<ll>
const ll mod = 998244353;
int n,cnt;bool ch[N];
ll c[N][N],k;int m;
ll f[N];// countbit 为 n 的有多少小于小 l
namespace matrix{
struct node{
int a[N][N];
node(int val = 0){
for(int i = 0;i <= n+1;++i)for(int j = 0;j <= n+1;++j)a[i][j] = 0;
if(val)for(int i = 0;i <= n+1;++i)a[i][i] = 1;
}
}g,b;
// 矩阵是 0~n+1
node operator*(node n1,node n2){
node ans;
for(int i = 0;i <= n+1;++i){
for(int k = 0;k <= n+1;++k){
for(int j = 0;j <= n+1;++j){
ans.a[i][j] = (ans.a[i][j]+1ll*n1.a[i][k]*n2.a[k][j]) % mod;
}
}
}
return ans;
}
}using namespace matrix;
node qpow(node base,ll e){
node res(1);
while(e){
if(e&1)res = res * base;
base = base * base;
e >>= 1;
}
return res;
}
// 主函数
int main(){
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
char tmp;
while(tmp = cin.get(),tmp == '0' || tmp == '1')ch[++n] = (tmp^48),cnt += ch[n];reverse(ch+1,ch+n+1);
cin >> k >> m;
for(int i = 0;i < N;++i){c[i][0] = 1;for(int j = 1;j <= i;++j)c[i][j] = (c[i-1][j-1]+c[i-1][j]) % mod;}
// 预处理
int pre = 0;
for(int i = n;i >= 1;--i){if(!ch[i])continue;// 从第 i 位开始小于
for(int j = 0;j < i;++j)f[j+pre] = (f[j+pre]+c[i-1][j])%mod;
++pre;
}
// 构造初始矩阵 b 和转移矩阵 g,注意是 k=1 的状态
if((cnt<<1) <= m)g.a[n+1][n+1] = 1;
if(cnt <= m)b.a[n+1][0] = 1;
for(int i = 0;i <= n;++i){
b.a[i][0] = f[i];for(int j = 0;i+j <= m;++j)g.a[i][j] = c[n][i];
if(i+cnt <= m)g.a[i][n+1] = f[i];
}
node c = qpow(g,k-1);c = c*b;
ll ans = 0;
for(int i = 0;i <= n+1;++i)ans = (ans + c.a[i][0]) % mod;
cout << ans;
return 0;
}