E. Segment Sum
题意
做法
坑点
代码
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
typedef long long ll;
const ll Mod = 998244353ll;
typedef pair<ll,ll> pll;
#define Se second
#define Fi first
ll l,r,k;
ll pow_[20];
int cal(ll x)
{
int ans=0;
while(x)
{
if(x&1) ans++;
x/=2;
}
return ans;
}
pll dp[20][1<<12][2];
ll a[20];
pll dfs(ll len,ll state,bool limit,bool lead)
{
if(len==0) return pll(1,0);//遍历结束,无需算对答案的贡献,但是要像正常数位dp一样统计满足条件的个数
if(!limit&&dp[len][state][lead].Se) return dp[len][state][lead];//注意带前导0的数位dp套路
pll ans=pll(0,0);
int up=limit?a[len]:9;
for(int i=0;i<=up;i++)
{
ll temp=state|((lead||i)<<i);
//若当前有前导0而且当前数位为0,则状态不更新
if(cal(temp)>k) continue;
//不满足答案的状态要去掉
pll tmp=dfs(len-1,temp,limit&&i==a[len],lead||i);
ans.Fi=(ans.Fi+tmp.Fi)%Mod;
ans.Se=(ans.Se+tmp.Se+(1LL*i*pow_[len-1]%Mod*tmp.Fi)%Mod)%Mod;
//当前数位对答案的贡献为当前数位所代表的值为i*pow[len-1]
//当前数位为i的数字个数为tmp.Fi
//总贡献为:i*pow_[len-1]%Mod*tmp.Fi
}
return dp[len][state][lead]=ans;
}
ll solve(ll x)
{
for(int i=0;i<20;i++)
{
for(int j=0;j<(1<<12);j++)
{
for(int l=0;l<2;l++)
{
dp[i][j][l].Fi=0;
dp[i][j][l].Se=0;
}
}
}
memset(a,0,sizeof(a));
int len=0;
while(x)
{
a[++len]=x%10;
x/=10;
}
return dfs(len,0,true,0).Se%Mod;
}
int main()
{
pow_[0]=1;
for(int i=1;i<20;i++) pow_[i]=pow_[i-1]*10%Mod;//每一位的贡献预处理
scanf("%lld%lld%lld",&l,&r,&k);
printf("%lld\n",(solve(r)-solve(l-1)+Mod)%Mod);
return 0;
}