数位dp学习笔记

· · 个人记录

当你需要统计一个大范围的数相关问题,且与数位相关,考虑使用数位 dp。通常而言,数位 dp 将基于数位地统计信息,并形成类似前缀的答案。有时,数位 dp 会基于递归实现。

\color{Blue} \text{看看模板题}

给出 [L,R]( R \le 10^{12}) 区间内,数字 0 \sim 9 各出现几次。

 

 

 

 

 

 

 

 

 

 

 

 

SOLUTION

当 R \le 10^6,可以暴力枚举所有 [L,R] 的数,拆出每一位,得到答案。复杂度 O(R \log R)。

但是枚举是不优的。例如这些数 213,247,235,288,273。可以发现,百位的 2 产生了 5 贡献。在暴力方法中,当我们枚举到这 5 个数时,拆开他们,之后再加,很慢。我们可不可以直接算出百位 2 会产生多少贡献呢?这就是数位 dp。

首先一个性质:当包含前导 0,所有 x 位数中,每个数字出现的次数相同。这点是显然的。例如,[00,99] 中,数字 0 \sim 9 都出现了 20 次。

设 dp_i 表示 i 位数任意数字的出现次数。例如 dp_2=20。

转移方程为 dp[i]=dp[i-1] \times 10+10^{i-1}。

等号右边容易理解。dp_{i-1} \times 10 统计的是第 [1,i-1] 位数字的贡献。10^{i-1} 则是当第 i 位是某个数时产生的贡献。

po[0]=1;//po[i]=pow(10,i)
for(int i=1;i<=13;i++)
{
    dp[i]=dp[i-1]*10+po[i-1];
    po[i]=po[i-1]*10;
}

这是基于 dp 的预处理。接下来,我们要算出 [L,R] 中的具体答案。我们发现,这个答案是 [1,R] 答案和 [1,L-1] 答案的差。

如何算出这个答案?

我们基于数位统计这个答案。从高到低上界 limit 的每一位(当前第 i 位,数字为 dig[i]),设 cnt[j] 代表数字 j 的出现次数:

#include <bits/stdc++.h>
using namespace std;
long long dp[15],cnt1[15],cnt2[15],po[15];
long long L,R;
void Count(long long limit,long long *cnt)
{
    long long rest=limit;
    int len=0,dig[15];
    while(limit)
    {
        dig[++len]=limit%10;
        limit/=10;
    }
    for(int i=len;i;i--)
    {
        for(int j=0;j<10;j++) cnt[j]+=dp[i-1]*dig[i];
        for(int j=0;j<dig[i];j++) cnt[j]+=po[i-1];
        rest-=po[i-1]*dig[i];
        cnt[dig[i]]+=rest+1;//
  rest是动态上界,相当于上面例子中的475
        cnt[0]-=po[i-1];
    }
    return;
}
int main()
{
    scanf("%lld %lld",&L,&R);
    po[0]=1;
    for(int i=1;i<=13;i++)
    {
        dp[i]=dp[i-1]*10+po[i-1];
        po[i]=po[i-1]*10;
    }
    Count(L-1,cnt1);
    Count(R,cnt2);
    for(int i=0;i<10;i++) printf("%lld ",cnt2[i]-cnt1[i]);
    puts("");
    return 0;
}

容易发现,时间复杂度 O(|C| \log N)。在这里 |C|=10。

\color{Blue} \text{Windy 数}

给出 [L,R]( R \le 2 \times 10^{9}) 区间内,有多少正整数(称为 \text{Windy} 数)满足相邻两位数字差 \ge 2。

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

SOLUTION

首先将答案转化为 ans(1,R)-ans(1,L-1)。

用 dfs 的方式,从高往低进行数位枚举,dfs 存 4 个参数:当前位数 step、上一位 pre、是否处在前导 0 位置 lead、是否有限制 lim。什么叫“有限制?” 当上界是 114514,当前枚举到 114...,那么下一位不应超过 5;如果枚举到 113...,那么下一位可以在 [0,9] 之间任选。dfs 的返回值就是参数规定的 \text{Windy} 数数量。

记忆化即可。 时间复杂度 O(|C| \log R)。

#include <bits/stdc++.h>
#define int long long
using namespace std;
int dp[15][15],cnt1[15],cnt2[15],po[15];
int dig[15];
int L,R;
int dpfs(int step,int pre,bool lead,bool lim)
{
    if(step==0) return 1;
    if(!lead&&!lim&&dp[step][pre]!=-1) return dp[step][pre];
    int mx=lim?dig[step]:9;
    int res=0;
    for(int i=0;i<=mx;i++)
    {
        if(abs(i-pre)<2) continue;
        res+=dpfs(step-1,(lead&&i==0)?-3:i,(lead&&i==0),(i==mx&&lim));
    }
    if(!lead&&!lim) dp[step][pre]=res;
    return res;
}
int Count(int limit)
{
    int len=0;
    while(limit) 
    {
        dig[++len]=limit%10;
        limit/=10;
    }
    memset(dp,-1,sizeof(dp));
    return dpfs(len,-3,1,1);
}
signed main()
{
    scanf("%lld %lld",&L,&R);
    printf("%lld\n",Count(R)-Count(L-1));
    return 0;
}

\color{Blue} \text{手机号码}

给出 [L,R]( 10^{10} \le L \le R < 10^{11}) 区间内,有多少 11 位数满足:

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

&nbsp;

SOLUTION

把上一题代码略加修改即可。

int dpfs(int step,int pre1,int pre2,bool lim,bool triple,bool four,bool eight)
{
    if(four&&eight) return 0;
    if(step==0) return triple;
    if(dp[step][pre1][pre2][lim][triple][four][eight]!=-1) return dp[step][pre1][pre2][lim][triple][four][eight];
    int res=0,mx=lim?dig[step]:9;
    for(int i=0;i<=mx;i++)
    {
        if(four&&i==8||eight&&i==4) continue;
        res+=dpfs(step-1,i,pre1,(lim&&i==mx),(triple||pre1==pre2&&pre1==i),(four||i==4),(eight||i==8));
    }
    dp[step][pre1][pre2][lim][triple][four][eight]=res;
    return res;
}