日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

数字计数

發布時間:2023/12/13 综合教程 17 生活家
生活随笔 收集整理的這篇文章主要介紹了 数字计数 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接:https://loj.ac/problem/10169

題意:給定兩個正整數 l 和 r,求在[l,r]中的所有整數中,每個數字各出現了多少次。

思路:數位dp,維護每個數字出現次數和數字個數即可,在計數時,比如十位是1,那1的貢獻還要加上個位所以可能的數。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
struct node
{
    ll s;
    ll b[10];
    node()
    {
        s=0;
        for(int i=0;i<10;i++)
            b[i]=0;
    }
}dp[30];
int a[30];
//pos : 位數  qd : 是否為前導  flag : 是否處于上邊界 
node dfs(int pos,bool qd,bool flag)
{
    if(pos==0)
    {
        node t;
        t.s=1;
        return t;
    }
    int up=flag?a[pos]:9;//確定上邊界
    if((!flag)&&(!qd)&&dp[pos].s)//如果之前已經求出,則直接返回
        return dp[pos];
    node tmp;
    tmp.s=0;
    for(int i=0;i<10;i++)
        tmp.b[i]=0;
    for(int i=0;i<=up;i++)
    {
        node t=dfs(pos-1,qd&&i==0,flag&&i==up);
        tmp.s+=t.s;
        for(int j=0;j<10;j++)
            tmp.b[j]+=t.b[j];
        if(!qd || i!=0)//如果此時已經不是前導0,那i出現的次數要加上t.s 
            tmp.b[i]+=t.s;
    }
    if((!flag)&&(!qd))
        dp[pos]=tmp;//記錄值,方便下次直接返回
    return tmp;
}
node fun(ll x)
{
    int pos=0;
    while(x)//將x的每一位進行分解
    {
        a[++pos]=x%10;
        x/=10;
    }
    return dfs(pos,true,true);
}
int main()
{
    ll l,r;
    cin>>l>>r;
    node x=fun(r);
    node y=fun(l-1);
    for(int i=0;i<10;i++)
        cout<<x.b[i]-y.b[i]<<" ";
    cout<<endl;
}

總結

以上是生活随笔為你收集整理的数字计数的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。