BZOJ1833 count 數字計數 數位dp基礎題

鏈接:

count 數字計數

思路:

最基礎的數位dp,甚至沒有限定特徵,直接貼板子就好。

代碼:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
static const int maxn = 60;
static const int INF = 0x3f3f3f3f;
static const int mod = (int)1e9 + 7;
static const double eps = 1e-6;
static const double pi = acos(-1);

void redirect(){
    #ifdef LOCAL
        freopen("test.txt","r",stdin);
    #endif
}

ll l,r,ans[maxn],dp[maxn][maxn][maxn],bin[maxn],d[maxn];

ll solve(ll x,int flag){
    int cnt = 0;
    ll bak = x;
    memset(d,0,sizeof(d));
    while(x){
        d[++cnt]=x%10;
        x/=10;
    }
    for(int i = 1;i < cnt;i++)
    for(int j = 1;j <= 9;j++)
    for(int k = 0;k <= 9;k++)
    ans[k] += dp[i][j][k] * flag;
    int tmp = cnt;
    while(tmp){
        for(int i = 0;i < d[tmp];i++){
            if(!i && tmp == cnt)continue;
            for(int j = 0;j <= 9;j++)
            ans[j] += dp[tmp][i][j] * flag;
        }
        ans[d[tmp]] += (bak % bin[tmp] + 1)*flag;
        tmp--;
    }
}

int main(){
    redirect();
    bin[1] = 1;
    for(int i = 2;i <= 13;i++)bin[i] = bin[i-1]*10;
    for(int i = 0;i <= 9;i++)dp[1][i][i]=1;
    for(int i = 2;i <= 13;i++)
    for(int j = 0;j <= 9;j++)
    for(int k = 0;k <= 9;k++){
    for(int z = 0;z <= 9;z++)
        dp[i][j][z] += dp[i-1][k][z];
        dp[i][k][k] += bin[i-1];
    }
    scanf("%lld %lld",&l,&r);
    solve(r,1);
    solve(l-1,-1);
    for (int i = 0;i <= 9;i++)
    printf("%lld%c",ans[i],i==9?'\n':' ');
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章