题目链接:Segment Sum
题意:求区间[l,r] (1≤l≤r<10^18) 内,符合以下条件的数的和:
满足[0,9]中数字出现不超过k(1≤k≤10)个
题解:一看这个题目就能联系到数位DP,刚开始直接用数位DP,在储存部分我用了mp记录,结果过不去,看了题解后发现数位DP的一个神奇用法,我们可以在经典数位DP的基础上把每次的返回值改成一个pair<ll,ll>,分别储存当前状态下往下能搜的方案数量,借助方案数量我们可以知道每一次转移时枚举的位数在总和中的贡献(我们将一个数的贡献拆分为在每一个位数上的数的贡献,这样每次转移时通过记录往下搜的方案数就能求出当前位置的总贡献),再加上往下搜的贡献即可得出当前状态下所以方案在该位置往下的部分的总贡献,其他部分就是正常数位DP
细节和不懂的可以看代码:
#include<iostream>
#include<stack>
#include<list>
#include<set>
#include<vector>
#include<algorithm>
#include<math.h>
#include<numeric>
#include<map>
#include<cstring>
#include<queue>
#include<iomanip>
#include<cmath>
#include<queue>
#include <bitset>
#include<unordered_map>
#ifndef local
#define endl '\n'
#endif */
using namespace std;
using std::bitset;
typedef long long ll;
typedef long double ld;
const int inf=0x3f3f3f3f;
const ll MAXN=1e6+10;
const ll N=1e5+100;
const ll mod=998244353;
const ll hash_p1=1610612741;
const ll hash_p2=805306457;
const ll hash_p3=402653189;
//-----------------------------------------------------------------------------------------------------------------*/
// ll head[MAXN],net[MAXN],to[MAXN],edge[MAXN]/*流量*/,cost[MAXN]//费用;
/*
void add(ll u,ll v,ll w,ll s){
to[++cnt]=v;net[cnt]=head[u];edge[cnt]=w;cost[cnt]=s;head[u]=cnt;
to[++cnt]=u;net[cnt]=head[v];edge[cnt]=0;cost[cnt]=-s;head[v]=cnt;
}
struct elemt{
int p,v;
};
struct comp{
public:
bool operator()(elemt v1,elemt v2){
return v1.v<v2.v;
}
};
priority_queue<elemt,vector<elemt>,comp>q;
*/
//-----------------------------------------------------------------------------------------------------------------*/
// unordered_map<int,int>mp;
pair<ll,ll> dp[20][2][1100];//状态压缩回溯记录,pair二维分别记录该状态下有多少种方案
//和这些方案在当前位数下方的值的和
ll a[100];
int k;
pair<ll,ll> check(ll d){//判断是否合法
int num=0;
for(int i=0;i<=9;i++){
if(d&(1<<i)){
num++;
if(num>k){
return {0,0};
}
}
}
return {1,0};
}
ll mi(ll a,ll b){
ll res=1;
while(b){
if(b%2){
res=res*a%mod;
}
a=a*a%mod;
b/=2;
}
return res;
}
pair<ll,ll> dfs(ll b/*进制*/,ll len/*当前枚举第几位*/,int f/*是否有前导零*/,ll state/*对应二进制为各数字状态*/,int limit/*是否处在边界*/){
pair<ll,ll> res={0,0};
if(!check(state).first){//剪枝,不符合的直接返回,不继续往下搜了
return {0,0};
}
if(!len){//经典数位DP
return check(state);//各位置都为0,则表示各位置都取了偶数次
}
if(!limit&&dp[len][f][state].first!=-1){//经典数位DP
return dp[len][f][state];
}
int top=b-1;
if(limit){
top=a[len];
}
for(ll i=0;i<=top;i++){
if(i==0&&f){
pair<ll,ll>tmp=dfs(b,len-1,1,state,limit&&(i==top));//经典数位DP
res.first+=tmp.first;//加上儿子的方案数
res.first%=mod;
res.second=(res.second+tmp.second+(tmp.first*i%mod)*mi(10,len-1)%mod)%mod;//递推方程
}
else{
pair<ll,ll>tmp=dfs(b,len-1,0,state|(1<<i),limit&&(i==top));//经典数位DP
res.first+=tmp.first;
res.first%=mod;
res.second=(res.second+tmp.second+(tmp.first*i%mod)*mi(10,len-1)%mod)%mod;
}
}
if(!limit){//记忆化标记
dp[len][f][state]=res;
}
return res;
}
ll cal(ll b,ll d){
int res=0;
while(d){
a[++res]=d%b;
d/=b;
}
return dfs(b,res,1,0,1).second;
}
int main(){
/*cout<<setiosflags(ios::fixed)<<setprecision(8)<<ans<<endl;//输出ans(float)格式控制为8位小数(不含整数部分)*/
/*cout<<setprecision(8)<<ans<<endl;//输出ans(float)格式控制为8位小数(含整数部分)*/
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//同步流
for(int i=0;i<20;i++){
for(int j=0;j<2;j++){
for(int p=0;p<1100;p++){//赋初始值
dp[i][j][p]={-1,-1};
}
}
}
ll b=10,l,r;
cin>>l>>r>>k;
cout<<(cal(b,r)+mod-cal(b,l-1))%mod<<endl;
return 0;
}
//数位dp求符合条件数的和