当前位置:网站首页>Digital statistics DP acwing 338 Counting problem
Digital statistics DP acwing 338 Counting problem
2022-07-03 09:05:00 【T_ Y_ F666】
Digital statistics DP AcWing 338. Counting problem
Original link
Ideas
For numbers n Each number has a number x Have a classified discussion
Fourth place here d For example
See code for details
Code
#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;
}
// Get array a The first r Bit to bit l Digit number
int get(vector<int> a, int l, int r){
int res=0;
Rep(i, l, r){
res=res*10+a[i];
}
return res;
}
// 1 to n in x Number of occurrences
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();
// if x by 0 n-2->0 Because the highest bit cannot be 0, So we should start from n-2 Start
// if x Not 0 n-1->0
Rep(i, n-1-!x, 0){
// front i digit ( namely abc) Composition less than n In the numbers x Number of occurrences
if(i<n-1){
res+=(get(a, n-1, i+1))*pow(i);
// Before removal i The number of digits is zero
if(!x){
res-=pow(i);
}
}
// The first i Bit after ( namely efg) Less than n In the numbers x Number of occurrences
if(a[i]==x){
res+=get(a, i-1, 0)+1;
}else if(a[i]>x){// The first i The number of all digits after the digit
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 To b in i Number of entries
printf("%lld ", cnt(b, i)-cnt(a-1, i));
}
puts("");
}
return 0;
}
Originality is not easy.
Reprint please indicate the source
If it helps you Don't forget to praise and support
边栏推荐
- 浅谈企业信息化建设
- Gif remove blank frame frame number adjustment
- LeetCode 1089. 复写零
- Apache startup failed phpstudy Apache startup failed
- Concurrent programming (III) detailed explanation of synchronized keyword
- 网络安全必会的基础知识
- LeetCode 75. Color classification
- createjs easeljs
- LeetCode 513. Find the value in the lower left corner of the tree
- 我们有个共同的名字,XX工
猜你喜欢
PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
LeetCode 532. K-diff number pairs in array
Too many open files solution
浅谈企业信息化建设
LeetCode 508. 出现次数最多的子树元素和
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
LeetCode 75. 颜色分类
AcWing 786. 第k个数
Complex character + number pyramid
JS non Boolean operation - learning notes
随机推荐
LeetCode 324. Swing sort II
LeetCode 30. 串联所有单词的子串
String splicing method in shell
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
Find the combination number acwing 886 Find the combination number II
AcWing 786. Number k
LeetCode 535. TinyURL 的加密与解密
Allocation exception Servlet
LeetCode 715. Range 模块
Sending and receiving of request parameters
Method of intercepting string in shell
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
精彩回顾|I/O Extended 2022 活动干货分享
Solution of 300ms delay of mobile phone
常见渗透测试靶场
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
Six dimensional space (C language)
LeetCode 57. 插入区间
Using DLV to analyze the high CPU consumption of golang process