题目描述
In a math class,you are assigned the following task.
First,look at the definition F(n,d)=k which means that in the given number n,there are k d's.
For instance,
F(311,1)=2,F(311,3)=1
For the given
(n,k,d)
you are to find how many i's which can meet the constraits below
(1)1<=i<=n
(2)F(i,d)=k
输入格式
Multiple lines ended with EOF
Each line contains three integers n(1<=n<2^31),k(1<=k<=9),d(1<=d<=9)
输出格式
A single integer
Hint
For the first case,there are 5 numbers (1,10,12,13,14)
For the second case,there are 2 numbers (22,122)
输入样例
14 1 1
123 2 2
876 2 3
输出样例
5
2
26
/*
USER_ID: test#buslt
PROBLEM: 182
SUBMISSION_TIME: 2014-03-07 19:04:24
*/
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define ll __int64
#define inf 0x3f3f3f3f
int n,k,d;
int bit[20];
int dp[20][20];
int dfs(int pos,int num,int flag)
{
if(num<0) return 0;
if(pos==-1) return num==0;
if(flag==0 && dp[pos][num]!=-1) return dp[pos][num];
int end=flag?bit[pos]:9;
int i,j,k,ans=0;
for(i=0;i<=end;i++)
ans+=dfs(pos-1,(i==d)?(num-1):num,flag&&i==end);
if(flag==0) dp[pos][num]=ans;
return ans;
}
int calc(int x)
{
int i,j,len=0;
while(x)
{
bit[len++]=x%10;
x/=10;
}
return dfs(len-1,k,1);
}
int main()
{
int i,j;
while(scanf("%d%d%d",&n,&k,&d)!=EOF)
{
memset(dp,-1,sizeof(dp));
printf("%d\n",calc(n));
}
}