[bzoj3679]數字之積
阿新 • • 發佈:2019-02-20
題目大意
一個數x各個數位上的數之積記為f(x) <不含前導零>
求[L,R)中滿足0 < f(x)<=n的數的個數
100% 0 < L < R < 10^18 , n<=10^9
分析
首先很容易想到數位DP
設f[i][j]表示各位乘積為j的i位數有多少個,轉移時列舉下一位的數即可。
然後求答案就相當於ans([1,R))-ans([1,L))。
但是n太大了,不過經過暴力計算,n等於
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N=20,M=5205;
typedef long long LL;
LL L,R,ans,f[N][M];
int n,tot,a[M];
map <int,int> t;
void dfs(LL x)
{
if (x>n || t.find(x)!=t.end()) return;
a[tot]=x;
t.insert(make_pair(x,tot++)); ans++;
for (int i=2;i<=9;i++) dfs(x*i);
}
LL calc(LL m)
{
if (!n) return 0;
LL now=1,p=1;
int i,j,k; ans=0;
for (i=0;p<=m/10;i++,p*=10);
for (j=1;j<=i;j++)
for (k=0;k<tot;k++) ans+=f[j][k];
for (;i>=0;i--,p/=10)
{
for (j=1;j<m/p;j++)
{
for (k=0;k<tot;k++)
{
if (now*j*a[k]<=n) ans+=f[i][k];
}
}
now*=m/p; m%=p;
if (!now || now>n) return ans;
}
return ans+(now<=n);
}
int main()
{
scanf("%d%lld%lld",&n,&L,&R);
dfs(1);
f[0][0]=1;
for (int i=1;i<N;i++)
{
for (int j=0;j<tot;j++) if (f[i-1][j]>0)
{
int tmp=a[j];
for (int k=1;k<10;k++) if (tmp<=n/k)
{
f[i][t.find(tmp*k)->second]+=f[i-1][j];
}
}
}
printf("%lld\n",calc(R-1)-calc(L-1));
return 0;
}