I. Counting 第八届北京邮电大学程序设计竞赛 - 热身赛 (1)

时间限制 1000 ms  内存限制 65536 KB

题目描述

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
数位dp..

/*
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));
    }
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值