BZOJ3679: 数字之积

Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 633 Solved: 274
[Submit][Status][Discuss]

Description

一个数x各个数位上的数之积记为f(x) <不含前导零>
求[L,R)中满足0<f(x)<=n的数的个数

Input

第一行一个数n
第二行两个数L、R

Output

一个数,即满足条件的数的个数

Sample Input

5

19 22

Sample Output

1

HINT

100% 0<L<R<10^18 , n<=10^9

Source

题解

一开始以为0也要算进去
但是仔细一看不用算0!!!
于是就变得不(非)是(常)很(套)难(路)

虽然我还是一遍过不了

不(题)难(解)发(上)现(说),由于乘积最多有2,3,5,7这几个因数,所以乘数不算太多,大概几千个。
于是可以先把乘数处理出来,编个号,\(dp[i][j]\)表示i位数,乘积为编号j所对应数的输的个数。
注意如果数位dp中要判不为0!

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
inline void swap(long long &x, long long &y){long long tmp = x;x = y;y = tmp;}
inline void read(long long &x)
{
    x = 0;char ch = getchar(), c = ch;
    while(ch < '0' || ch > '9') c = ch, ch = getchar();
    while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
    if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f;
const long long dx[4] = {2, 3, 5, 7};
std::map<long long, long long> mp;
long long remp[6000];
long long dp[20][6000], n, tot, num[20];
void yuchuli(long long now, long long pre)
{
	for(long long i = pre;i < 4;++ i)
	{
		if(now * dx[i] > n) break;
		mp[now * dx[i]] = ++ tot;
		remp[tot] = now * dx[i];
		yuchuli(now * dx[i], i);
	}
}
long long tiaoshi;
long long solve(long long x)
{
	if(x == 0) return 0;
	long long re = 0, M = 0, pre;
	long long tmp = x;
	while(tmp) num[++ M] = tmp % 10, tmp /= 10;
	for(long long j = 1;j <= M - 1;++ j)
		for(long long i = 1;i <= tot;++ i)
			if(remp[i] <= n)
				re += dp[j][i];
	pre = num[M];
	for(long long i = 1;i < num[M];++ i)
		for(long long j = 1;j <= tot;++ j)
			if(i * remp[j] <= n)
				re += dp[M - 1][mp[remp[j]]];
	for(long long i = M - 1;i >= 2;-- i)
	{
		for(long long k = 1;k < num[i];++ k)
			for(long long j = 1;j <= tot;++ j)
				if(pre * k * remp[j] <= n)
					re += dp[i - 1][mp[remp[j]]];
		pre *= num[i];
		if(!pre || pre > n) break;
	}
	for(long long i = 1;i <= num[1];++ i)
		if(pre * i <= n && pre * i) ++ re;
	return re;
}
int main()
{
	mp[1] = ++ tot, remp[tot] = 1;
	read(n);
	yuchuli(1, 0);
	
	for(long long i = 1;i <= 9;++ i) if(i <= n) dp[1][mp[i]] = 1;
	for(long long i = 1;i <= 17;++ i)
		for(long long j = 1;j <= tot;++ j)
			for(long long k = 1;k <= 9;++ k)
				dp[i + 1][mp[remp[j] * k]] += dp[i][j];
	long long a,b;
	read(a), read(b);
	if(a > b) swap(a, b);
	printf("%lld\n", solve(b - 1) - solve(a - 1));
    return 0;
}
posted @ 2018-03-16 07:15  嘒彼小星  阅读(521)  评论(0编辑  收藏  举报