跳到正文

题解

【题解】AT_abc208_e [ABC208E] Digit Products

YuYuanPQ约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年4月28日
收录
2025年12月12日
更新
2026年11月18日
篇幅
约 3 分钟
获赞
1

版本

  1. v1当前

注意到要求的是区间内某种数的个数,数据范围很大。
考虑数位 DP。

设 fx,sumf_{x,sum} 表示当前到第 xx 位(倒序枚举),数字乘积为 sumsum 时,满足 sum≤ksum\leq k 的数的个数。
但是你会发现 sumsum 很大,又显然可以想到其实出现的值并不多,所以可以用 map 来存。

最费脑子的也就这里:

  • 当有前导零限制时:
    • 若 i=0i=0,这意味着还没有非 00 数出现,所以 sumsum 不变(还是 00)。
    • 若 i≠0i\neq 0,这就是第 11 个有效位,令 sum=isum=i。
  • 没有限制时:sumsum 就乘上 ii。

然后套数位 DP 的模板就好啦!


呃呃,这题赛时想到了正解但没敢打…(还因为当时已经不是很熟了)
而且赛后发现我确实不完全会打数位 DP 模板。
还不是因为自己不够努力。

Code

C++
#include<bits/stdc++.h>

typedef int IT;
typedef long long LL;
typedef __int128 int128;
typedef double DB;
typedef long double LDB;

#define pb push_back
#define fst first
#define sec second
#define psh push
#define mkp make_pair
#define PII pair<IT,IT>
#define PLI pair<LL,IT>
#define lowbit(x) ((x)&(-x))

using namespace std;

const int WS=18;

LL n;
int k;

int d[WS+5],dl;
unordered_map<LL,LL>f[WS+3];

LL dfs(int x,LL sum,bool lim,bool zer){
    if(!x) return sum<=k;
    if(!lim&&!zer&&f[x].count(sum)) return f[x][sum];
    LL cnt=0;
    for(int i=0;i<=(lim?d[x]:9);i++){
        if(zer){
            if(!i) cnt+=dfs(x-1,sum,lim&&(i==d[x]),1);
            else cnt+=dfs(x-1,i,lim&&(i==d[x]),0);
        }
        else{
            cnt+=dfs(x-1,sum*i,lim&&(i==d[x]),0);
        }
    }
    if(!lim&&!zer) f[x][sum]=cnt;
    return cnt;
}

LL calc(LL x){
    dl=0;
    while(x){
        d[++dl]=x%10;
        x/=10;
    }
    return dfs(dl,0,1,1)-1;
}

int main(){
    scanf("%lld%d",&n,&k);
    printf("%lld\n",calc(n));
    return 0;
}

Link

原文