UVA 11038 How Many O's(数位dp)

题意:查询区间[n,m]中的数有多少个0。

思路:数位dp,dp[i][j][k]表示前i位,已经有了j个0,前面是否都是0,这样的数的所有0的个数。

代码:


#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<stack>
#include<set>
#include<cmath>
#include<vector>
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define eps 1e-9
#define pi acos(-1.0)
using namespace std;
typedef long long ll;
int num[20];
ll dp[20][20][2];
ll f(int pos,int cnt,int st,bool limit)
{
    if(pos==-1) return st?cnt:1;
    if(!limit&&dp[pos][cnt][st]!=-1) return dp[pos][cnt][st];
    int last=limit?num[pos]:9;
    ll ans=0,tmp;
    for(int i=0;i<=last;++i)
    {
        tmp=cnt;
        if(st&&i==0) tmp++;
        ans+=f(pos-1,tmp,st|(i!=0),limit&&i==last);
    }
    if(!limit) dp[pos][cnt][st]=ans;
    return ans;
}
ll cal(ll x)
{
    if(x==-1) return 0;
    int len=0;
    while(x)
    {
        num[len++]=x%10;
        x/=10;
    }
    return f(len-1,0,0,true);
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ll m,n;
    while(~scanf("%lld%lld",&m,&n))
    {
        if(m==-1&&n==-1) break;
        memset(dp,0xff,sizeof(dp));
        printf("%lld\n",cal(n)-cal(m-1));
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值