zoukankan      html  css  js  c++  java
  • 数字计数

    题意:问[a,b]中0-9的数码各出现了多少次。a,b≤1012

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #define LL long long 
    using namespace std;
    
    inline LL read()
    {
        LL f=1,x=0;
        char ch=getchar();
        while(ch<'0' || ch>'9') {if(ch=='-') f=-1; ch=getchar();}
        while(ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
        return x*f;
    }
    
    LL l,r;
    LL dp[15][15][2][2];
    int a[15],q;
    
    LL dfs(int pos,int sum,bool flag,bool done)
    {
        if(pos==0)
            return sum;
        if(!done && dp[pos][sum][flag][done]!=-1)
            return dp[pos][sum][flag][done];
        int r=done ? a[pos] : 9;
        LL res=0;
        for(int i=0;i<=r;i++)
        {
            res+=dfs(pos-1,sum+((!flag || i) && i==q),flag && i==0,done && i==a[pos]);
        }
        if(dp[pos][sum][flag][done]=-1)
            dp[pos][sum][flag][done]=res;
        return res;
    }
    
    LL init(LL x)
    {
        memset(dp,-1,sizeof(dp));
        int cnt=0;
        while(x>0)
        {
            a[++cnt]=x%10;
            x/=10;
        }
        return dfs(cnt,0,1,1);
    }
    
    int main()
    {
        l=read(); r=read();
        for(int i=0;i<=9;i++)
        {
            q=i;
            printf("%lld ",init(r)-init(l-1));
        }
        return 0;
    }
  • 相关阅读:
    用户体验
    jsp ini 配置文件
    Highcharts 图表
    简单游戏服务器
    js 数据操作
    jquery 导航
    as3.0 删除子元件
    java float 保留二位小数
    局域网ip
    SWFUpload
  • 原文地址:https://www.cnblogs.com/llllllpppppp/p/10097211.html
Copyright © 2011-2022 走看看