1591:数字计数

【题目描述】
原题来自:ZJOI 2010

给定两个正整数 a 和 b,求在 [a,b] 中的所有整数中,每个数码 (digit) 各出现了多少次。

【输入】
仅包含一行两个整数 a,b,含义如上所述。

【输出】
包含一行 10 个整数,分别表示 0∼9 在 [a,b] 中出现了多少次。

【输入样例】
1 99
【输出样例】
9 20 20 20 20 20 20 20 20 20
【提示】
数据范围与提示:

30% 的数据中,1≤a≤b≤106 ;

%100% 的数据中,1≤a≤b≤1012 。

#include<iostream>
#include<cstring>
using namespace std;
long long  digit[25];
#define D digit[0]
#define ll long long
struct node
{
    ll f[10],index;
    node(){
    for(ll i=0;i<10;i++)
        f[i]=0;
    index=0;
    }
}dp[25][25];
node dfs(ll pos,bool done,ll res,bool p)
{
    if(!pos){
        node ps;
        if(p)
        ps.index=1;
        return ps;
    }
    if(!done&&p&&dp[pos][res].index)
        return dp[pos][res];
    ll ends=done?digit[pos]:9;
    node now,ans;
    ans.index=0;
    for(ll i=0;i<=ends;i++)
    {
        now=dfs(pos-1,done&&i==ends,i,p|i);
            ans.index+=now.index;
            if(p|i)
            ans.f[i]+=now.index;
           for(ll j=0;j<10;j++)
                ans.f[j]+=now.f[j];
    }
    if(!done&&p)dp[pos][res]=ans;
    return ans;
}
node get_digit(ll x)
{
    D=0;
    while(x)
    {
        digit[++D]=x%10;
        x/=10;
    }
    return dfs(D,1,0,0);
}

int main()
{
    ll a,b;
    cin>>a>>b;
    node now,ing;
    now=get_digit(b);
    ing=get_digit(a-1);
    for(ll i=0;i<10;i++){
        cout<<now.f[i]-ing.f[i]<<' ';
    }
    cout<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值