Description
Given three integers a, b, P, count the total number of digit P appearing in all integers from a to b.
Input
The first line is a single integer T, indicating the number of test cases.
For each test case:
There are only three numbers
a,b,P (0≤a≤b≤231-1, 0
Output
For each test cases:
Print the total appearances of digit P from a to b in a single line.
Sample Input
2
1 10000 1
1 10 1
Sample Output
4001
2
题意
嗯 给一个区间和一个数字 求区间内一共有多少个数字
题解:
数位DP板子题 第一次打玲珑杯遇到的 当时想了各种姿势wa 时隔版半年终于抄板子A了对依水可喜可贺啊2333
AC代码
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define ll long long
const int mod = 1e9+7;
ll dp[100];
ll Pow (ll a, ll b) //手写 pow函数 防止精度缺失
{
ll ans = 1;
while(b) {
if(b&1) ans = ans*a;
b /= 2;
a = a*a;
}
return ans;
}
void init()
{
memset(dp,0,sizeof(dp));
for(int i = 1;i <= 18; i++) {
dp[i] = i*Pow(10,i-1); //计算出现的次数 比如00 到 99 都是20
}
}
ll sum(ll x, ll k)
{
ll radix = 1, tail = 0, res = 0;
ll ans = x;
ll digit = 0, len = 0;
while(x) {
len++;
digit = x%10;
x /= 10;
if(digit > k) {
res += radix + digit*dp[len-1];;
}
else if(digit == k) {
res += tail + 1 + digit*dp[len-1];
}
else {
res += digit*dp[len-1];
}
tail += digit*radix;
radix *= 10;
}
if(k == 0) { // 去除重复的个数 比如 000-999 每次都重复计算了0的个数 需要减掉
ll m = 1;
while(ans) {
ans /= 10;
res -= m;
m *= 10;
}
}
return res;
}
int main()
{
int T;
scanf("%d",&T);
while(T--) {
ll a, b;
int x;
scanf("%lld%lld%d",&a,&b,&x);
init();
printf("%lld\n",sum(b,x)-sum(a-1,x));
}
return 0;
}