跳转至

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;
}