codeforces-55D-Beautiful numbers(数位DP)


A - Beautiful numbers
Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.

Input

The first line of the input contains the number of cases t (1 ≤ t ≤ 10). Each of the next t lines contains two natural numbers li and ri(1 ≤ li ≤ ri ≤ 9 ·1018).

Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).

Output

Output should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).

Sample Input

Input
1
1 9
Output
9
Input
1
12 15
Output
2

F:

题意:求区间[x , y]中beautiful number的个数,a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits.

分析:一个数能被它的所有非零数位整除,则能被它们的最小公倍数整除,而1到9的最小公倍数为2520,数位DP时我们只需保存前面那些位的最小公倍数就可进行状态转移,到边界时就把所有位的lcm求出了,为了判断这个数能否被它的所有数位整除,我们还需要这个数的值,显然要记录值是不可能的,其实我们只需记录它对2520的模即可,这样我们就可以设计出如下数位DP:dfs(pos,mod,lcm,f),pos为当前位,mod为前面那些位对2520的模,lcm为前面那些数位的最小公倍数,f标记前面那些位是否达到上限,这样一来dp数组就要开到19*2520*2520,明显超内存了,考虑到最小公倍数是离散的,1-2520中可能是最小公倍数的其实只有48个,经过离散化处理后,dp数组的最后一维可以降到48,这样就不会超了。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<limits.h>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<math.h>
#define MOD 2520
__int64 dp[20][MOD][48];
__int64 digit[20];
int cnt,t[200];
int gcd(int a,int b)
{
	int temp;
	if(b==0)
	  return a;
	if(a<b)
	{
		temp=a;
		a=b;
		b=temp;
	}
	return gcd(b,a%b);
}
int LCM(int a,int b)
{
	return a/gcd(a,b)*b;
}
int bs(int x)
{
	int mid,min=0,max=cnt;
	while(min+1!=max)
    {
        mid=min+max>>1;
        if(t[mid]>x)    max=mid;
        else    min=mid;
    }
    return min;
}
__int64 dfs(int pos,int mod,int lcm,int limit)
{
	int i,num;
	if(pos<=0)
	  return (mod%t[lcm])?0:1;
	if(limit==0 && dp[pos][mod][lcm]!=-1)
	  return dp[pos][mod][lcm];
	num=limit?digit[pos]:9;
	__int64 ans=0;
	for(i=0;i<=num;i++)
	{
		int nmod=(mod*10+i)%MOD;
		int nlcm=lcm;
		if(i)
		  nlcm=bs(LCM(t[lcm],i));
		  ans+=dfs(pos-1,nmod,nlcm,limit&&i==num);
	}
	if(!limit)
	 dp[pos][mod][lcm]=ans;
	 return ans;
}
void init()
{
    cnt=0;
    for(int i=1;i<=MOD;i++)
    {
        if(MOD%i==0)t[cnt++]=i;
    }
    memset(dp,-1,sizeof(dp));
}
int main()
{
	int T,len;
	init();
	__int64 n,m;
	scanf("%d",&T);
	while(T--)
	{
		scanf("%I64d%I64d",&n,&m);
		len=0;
		while(m)
		{
			digit[++len]=m%10;
			m/=10;
		}
		__int64 ans2=dfs(len,0,0,1);
		n=n-1;
		len=0;
		while(n)
		{
			digit[++len]=n%10;
			n/=10;
		}
		__int64 ans1=dfs(len,0,0,1);
		printf("%I64d\n",ans2-ans1);
	}
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值