zoukankan      html  css  js  c++  java
  • LightOJ

    1140 - How Many Zeroes?

    吉米写下m和n之间的所有自然数的小数表示,(mωn)。他将写多少个零?

    思路:

    数位DP

    dp[pos][j]表示第pos位含有j个0的0的个数

    #include<bits/stdc++.h>
    
    #define LL long long
    using namespace std;
    
    LL T,l,r,shu[20],dp[20][20];
    
    LL dfs(LL pos,LL tot,bool lead,bool limit){
        if(pos==0) return lead?1:tot;//当它枚举到最后一位还有前导零的话,那只能是0,处理0的情况
        if(!lead&&!limit&&dp[pos][tot]) return dp[pos][tot];
        LL cnt=0,up=limit?shu[pos]:9;
        for(LL i=0;i<=up;i++){
            cnt+=dfs(pos-1,tot+(i==0&&!lead),lead&&i==0,limit&&i==shu[pos]);
        }
        if(!lead&&!limit) dp[pos][tot]=cnt;
        return cnt;
    }
    
    LL slove(LL x){
        LL k=0;
        while(x){
            shu[++k]=x%10;
            x/=10;
        }
        return dfs(k,0,true,true);
    }
    
    int main()
    {
        scanf("%lld",&T);
        for(LL i=1;i<=T;i++){
            scanf("%lld%lld",&l,&r);
            memset(dp,0,sizeof(dp));
            printf("Case %lld: %lld
    ",i,slove(r)-slove(l-1));
        }
        return 0;
    }
  • 相关阅读:
    Next Permutation
    Generate Parentheses
    Unique Binary Search Trees II
    LDP LSP建立
    LDP标签分发和管理
    维护LDP会话
    LDP会话状态机
    LDP会话建立过程
    LDP发现机制
    LDP术语
  • 原文地址:https://www.cnblogs.com/song-/p/9615274.html
Copyright © 2011-2022 走看看