2020-09-09 数位DP

http://icpc.upc.edu.cn/problem.php?id=14643

题目描述

Find the number of integers between 1 and N (inclusive) that contains exactly K non-zero digits when written in base ten.
Constraints
·1≤N<10100
·1≤K≤3

输入

Input is given from Standard Input in the following format:
N
K

输出

Print the count.

样例输入

【样例1】
100
1
【样例2】
25
2
【样例3】
314159
2
【样例4】
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
3

样例输出

【样例1】
19
【样例2】
14
【样例3】
937
【样例4】
117879300

提示

样例1解释
The following 19 integers satisfy the condition:1,2,3,4,5,6,7,8,9,10,20,30,40,50,60,70,80,90,100
样例2解释
The following 14 integers satisfy the condition:11,12,13,14,15,16,17,18,19,21,22,23,24,25

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[300][30]={0};
char a[1005]={0};
int main()
{
    ll k;
    scanf("%s%lld",a+1,&k);
    ll n=strlen(a+1);
    dp[0][0]=1;
    for(int i=1;i<=200;i++)
    {
        dp[i][0]=dp[i-1][0];
        for(int j=1;j<=20;j++)
            dp[i][j]=dp[i-1][j]+9*dp[i-1][j-1];
    }
    ll sum=0,cnt=0;
    for(int i=1;i<=n;i++)
    {
        if(a[i]>'0'&&k-cnt>=0)
            sum+=dp[n-i][k-cnt];
        if(a[i]>'1'&&k-1-cnt>=0)
            sum+=(a[i]-'0'-1)*dp[n-i][k-1-cnt];
        if(a[i]>'0')
            cnt++;
    }
    if(cnt==k)
        sum++;
    cout<<sum<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值