【玲珑 OJ 1067 Digital Count】+ 数位 dp

10 篇文章 0 订阅
2 篇文章 0 订阅

1067 - Digital Count
Time Limit:1s Memory Limit:1024MByte

Submissions:507Solved:111

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
T, indicating the number of test cases.

For each test case:

There are only three numbers
a
,
b
,
P
a,b,P (0≤a≤b≤231-1, 0 < P < 10).
OUTPUT
For each test cases:

Print the total appearances of digit
P
P from
a
a to
b
b in a single line.
SAMPLE INPUT
2
1 10000 1
1 10 1
SAMPLE OUTPUT
4001
2
SOLUTION

玲珑杯”ACM比赛 Round #6

题意 : 统计 l ~ r 内数字 o 出现了多少次

思路 : 数位 dp

AC代码:

#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
int s[20];
LL dp[30][30],p;
LL js(int n,int a,int b,int c,int k){
    if(n < 0){
        if(!c) return a;
        return k >= p ? 1 : 0;
    }
    if(!c && !b && dp[n][a] != -1) return dp[n][a]; // 剪枝,当不是临界值或者第一追溯时,不必再计算
    int z = b ? s[n] : 9; // 临界
    LL ans = 0;
    for(int i = 0; i <= z; i++){
        if(c && !i) ans += js(n - 1,0,b && i == z,1,k); // 第一次追溯
        else if(i == p) ans += js(n - 1,a + 1,b && i == z,0,k); // 统计数再次出现
        else  ans += js(n - 1,a,b && i == z,0,k);
    }
    if(!c && !b) dp[n][a] = ans; // 剪枝
    return ans;
}
LL sl(LL a){
    if(a < p) return 0;
    if(a >= p && a <= 9) return 1;
    memset(dp,-1,sizeof(dp));
    int m  = 0;
    while(a){
        s[m++] = a % 10;
        a /= 10;
    }
    return js(m - 1,0,1,1,0);
}
int main()
{
    int T,nl = 0;
    LL n,m;
    scanf("%d",&T);
    while(T--){
        scanf("%lld %lld %lld",&n,&m,&p);
        printf("%lld\n",sl(m) - sl(n - 1));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值