当前位置:网站首页>数位统计DP AcWing 338. 计数问题
数位统计DP AcWing 338. 计数问题
2022-07-03 08:41:00 【T_Y_F666】
数位统计DP AcWing 338. 计数问题
原题链接
思路
对于数字n每位数出现数字x进行分类讨论
此处以第四位d为例
详细过程见代码
代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 15;
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
int pow(int x){
int res=1;
while(x--){
res*=10;
}
return res;
}
// 获取数组a第r位至第l位数字
int get(vector<int> a, int l, int r){
int res=0;
Rep(i, l, r){
res=res*10+a[i];
}
return res;
}
// 1至n中x出现的次数
int cnt(int n, int x){
if(!n){
return 0;
}
else{
vector<int> a;
while(n){
a.push_back(n%10);
n/=10;
}
int res=0;
n=a.size();
// 若x为0 n-2->0 由于最高位不能位0, 故应该从n-2开始
// 若x非0 n-1->0
Rep(i, n-1-!x, 0){
// 前i位数(即abc)构成小于n数字中x出现次数
if(i<n-1){
res+=(get(a, n-1, i+1))*pow(i);
// 去除前i位数都为零个数
if(!x){
res-=pow(i);
}
}
// 第i位之后(即efg)小于n数字中x出现次数
if(a[i]==x){
res+=get(a, i-1, 0)+1;
}else if(a[i]>x){// 第i位后全部数字个数
res+=pow(i);
}
}
return res;
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int a, b;
while(a=read(), b=read(), a){
if(a>b){
swap(a, b);
}
rep(i, 0, 10){
// a到b中i出项次数
printf("%lld ", cnt(b, i)-cnt(a-1, i));
}
puts("");
}
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Monotonic stack -503 Next bigger Element II
- [public key cryptography] ECC elliptic cryptosystem (implementing ElGamal encryption method)
- 【Rust笔记】06-包和模块
- Simple demo of solving BP neural network by gradient descent method
- [set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
- Baidu editor ueeditor changes style
- 【Rust 笔记】08-枚举与模式
- 【Rust 笔记】11-实用特型
- 高斯消元 AcWing 883. 高斯消元解线性方程组
- Unity interactive water ripple post-treatment
猜你喜欢
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
[concurrent programming] consistency hash
求组合数 AcWing 886. 求组合数 II
JS ternary operator - learning notes (with cases)
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
二进制转十进制,十进制转二进制
Monotonic stack -84 The largest rectangle in the histogram
Message queue for interprocess communication
Unity Editor Extension - drag and drop
随机推荐
Eating fruit
Query XML documents with XPath
createjs easeljs
【Rust 笔记】11-实用特型
[redis] redis persistent RDB vs AOF (source code)
Deeply understand the underlying data structure of MySQL index
Unity editor expansion - the framework and context of unity imgui
Unity editor expansion - window, sub window, menu, right-click menu (context menu)
Osganimation library parsing
如何应对数仓资源不足导致的核心任务延迟
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Thymeleaf 404 reports an error: there was unexpected error (type=not found, status=404)
单调栈-503. 下一个更大元素 II
Convert video to GIF
Message queue for interprocess communication
On the difference and connection between find and select in TP5 framework
How to use Jupiter notebook
Concurrent programming (III) detailed explanation of synchronized keyword
Redux - learning notes
UE4 source code reading_ Mobile synchronization