【题解】AT_abc208_e [ABC208E] Digit Products

· · 题解

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

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

最费脑子的也就这里:

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

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

Code

#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