【BZOJ1833】【ZJOI2010】count 数字计数 (数位DP)

90 篇文章 0 订阅
83 篇文章 0 订阅

Description

给定 a,b a , b ,求出 a a b之间所有的整数中所有的数字出现个数和。

Solution

fi,j,k f i , j , k 为以 j j 开头、长度为i的所有数(允许前导0)中数字 k k 的出现次数。
预处理出fi,j,k,然后数位DP即可。
感觉做复杂了点吧 ,应该有更简单的做法。

Source

/****************************
 * Au: Hany01
 * Prob: [BZOJ1833][ZJOI2010] count 数字计数
 * Date: Feb 19th, 2018
 * Email: hany01@foxmail.com
****************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef vector<int> VI;
#define Rep(i , j) for (register int i = 0 , i##_end_ = j; i < i##_end_ ; ++ i)
#define For(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i <= i##_end_ ; ++ i)
#define Fordown(i , j , k) for (register int i = (j) , i##_end_ = (k) ; i >= i##_end_ ; -- i)
#define Set(a , b) memset(a , b , sizeof(a))
#define SZ(a) ((int)(a.size()))
#define ALL(a) a.begin(), a.end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define Mod (1000000007)
#define y1 wozenmezhemecaia 
#ifdef hany01
#define debug(...) fprintf(stderr , __VA_ARGS__)
#else
#define debug(...)
#endif

inline void File() {
#ifdef hany01 
    freopen("bzoj1833.in" , "r" , stdin);
    freopen("bzoj1833.out" , "w" , stdout);
#endif
}

template<typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template<typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }

inline LL read() {
    register char c_; register LL _ , __;
    for (_ = 0 , __ = 1 , c_ = getchar() ; !isdigit(c_) ; c_ = getchar()) if (c_ == '-')  __ = -1;
    for ( ; isdigit(c_) ; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
    return _ * __;
}

struct List { LL a[10]; }null;
List operator + (List L1, List L2) {
    List L3;
    Rep(i, 10) L3.a[i] = L1.a[i] + L2.a[i];
    return L3;
}

int T, a[15], pos;
LL n[2], Pow[15];
List cnt[2], dp[15], f[15][15];

inline void Init()
{
    Pow[0] = 1;
    For(i, 1, 12) Pow[i] = Pow[i - 1] * 10ll;
    Rep(i, 10) f[1][i].a[i] = 1;
    For(i, 2, 12) Rep(j, 10) Rep(k, 10)
        f[i][k] = f[i][k] + f[i - 1][j], f[i][k].a[k] += Pow[i - 1];
}

List dfs(int pos, int pre, int lmt)
{
    if (!pos) return null;
    if (!lmt && pre >= 0 && dp[pos].a[0] > 0) return dp[pos];
    List tmp = null;
    if (pre == -1) tmp = tmp + dfs(pos - 1, -1, 0); else {
        tmp = tmp + dfs(pos - 1, 0, lmt && !a[pos]);
        if (lmt && !a[pos]) tmp.a[0] += n[T] % Pow[pos - 1] + 1; else tmp.a[0] += Pow[pos - 1];
    }
    For(i, 1, lmt ? a[pos] : 9) {
        tmp = tmp + dfs(pos - 1, i, lmt && a[pos] == i);
        if (lmt && i == a[pos]) tmp.a[i] += n[T] % Pow[pos - 1] + 1; else tmp.a[i] += Pow[pos - 1];
    }
    if (!lmt && pre >= 0) dp[pos] = tmp;
    return tmp;
}

inline void Solve(int t)
{
    LL tmp = n[t];
    pos = 0;
    while (tmp) a[++ pos] = tmp % 10, tmp /= 10;
    cnt[t] = dfs(pos, -1, 1);
}

int main()
{
    File();
    Init();
    n[0] = read() - 1, n[1] = read();
    T = 0, Solve(0);
    T = 1, Solve(1);
    Rep(i, 9) printf("%lld ", cnt[1].a[i] - cnt[0].a[i]);
    printf("%lld\n", cnt[1].a[9] - cnt[0].a[9]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值