ABC 208 E - Digit Products
E - Digit Products
数位dp
因为只能由 1~9 相乘而来,所以 , , 所以最多 种乘积,总复杂度为
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#include <map>
using namespace std;
#define endl "\n"
typedef long long ll;
typedef pair<int, int> PII;
const int N = 20;
int pos;
int bound[N];
ll n, k;
//用map存第四维(乘积),因为乘积可能很大,但个数并不多(只能由2,3,5,7 四个质数相乘而来)
map<ll, ll> f[N][2][2];
ll dfs(int pos, ll mul, bool limit, bool zero)
{
if (pos <= 0)
return !zero && mul <= k;
if (f[pos][limit][zero].count(mul))
return f[pos][limit][zero][mul];
int maxn = limit ? bound[pos] : 9;
ll now = 0;
for (int i = 0; i <= maxn; i++)
{
//不能这样写,因为当前乘积如果大于k,但之后 *0 后就满足条件了,要在递归出口判断
// if (mul * i > k)
// continue;
if (zero && i == 0)
now += dfs(pos - 1, mul, limit && i == bound[pos], true);
else
now += dfs(pos - 1, mul * i, limit && i == bound[pos], false);
}
if (limit)
return now;
return f[pos][limit][zero][mul] = now;
}
ll solve(ll x)
{
pos = 0;
do
{
bound[++pos] = x % 10;
x /= 10;
}while(x);
return dfs(pos, 1, true, true);
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
cin >> n >> k;
cout << solve(n) << endl;
return 0;
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?