首页 技术 正文
技术 2022年11月14日
0 收藏 790 点赞 4,588 浏览 857 个字

题目大意:求 [a,b] 中 0-9 分别出现了多少次。

题解:看数据范围应该是一个数位dp。

在 dfs 框架中维护当前的位置和到当前位置一共出现了多少个 \(x,x\in [0,9]\)。因此,用一个 dp[][] 数组记录一下状态即可,dp 的含义大概是前 i 位中出现了 j 个 x 的总 x 的个数是多少。

代码如下

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;LL dp[15][15];
int digit[15],tot,now;LL dfs(int cur,int sum,bool lead,bool limit){
if(cur==0)return sum;
if(!lead&&!limit&&dp[cur][sum]!=-1)return dp[cur][sum];
LL ret=0;
int bit=limit?digit[cur]:9;
for(int i=0;i<=bit;i++){
if(!i&&lead)ret+=dfs(cur-1,sum,1,limit&&i==bit);
else if(i==now)ret+=dfs(cur-1,sum+1,0,limit&&i==bit);
else ret+=dfs(cur-1,sum,0,limit&&i==bit);
}
if(!lead&&!limit)dp[cur][sum]=ret;
return ret;
}
LL part(LL x){
tot=0;
memset(digit,0,sizeof(digit));
do{
digit[++tot]=x%10;
x/=10;
}while(x);
memset(dp,-1,sizeof(dp));
return dfs(tot,0,1,1);
}
int main(){
LL a,b;
cin>>a>>b;
for(int i=0;i<9;i++)now=i,printf("%lld ",part(b)-part(a-1));
now=9,printf("%lld\n",part(b)-part(a-1));
return 0;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,556
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898