codeforces 55D Beautiful numbers

D. Beautiful numbers
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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 test(s)
input
1
1 9
output
9
input
1
12 15
output
2
 
 题目大意:求一个区间内的"Beautiful numbers"有多少个。Beautiful numbers:一个数能整除所有组成它的非0数字。
 分析:套用数位dp
 一个数字要被它的所有非零位整除,即被他们的LCM整除。2~9的最小公倍数是2520,并且由2~9组成的最小公倍数只有48种。
 按照定义,数字x为Beautiful numbers:
    x % LCM{a[xi]} = 0
    即 x % MOD % LCM{digit[xi]} = 0
    所以可以只需存x % MOD,范围缩小了
    而在逐位统计时,假设到了pre***(pre指前面的一段已知的数字,而*是任意变)
        ( preSum * 10^pos + next )  % MOD % LCM(preLcm , nextLcm)
    =  ( preSum * 10 ^ pos % MOD + next % MOD ) % LCM(preLcm , nextLcm)
    == 0
构造dfs,除了原有参数i(当前位置),e(是否可达边界)。另外记录preLcm(当前最小公倍数),preSum(当前表示的数%2520);
#include<cstdio>
#include<cstring>
#define ll __int64
const int LCM=2520;
int a[19],b[LCM+1];
ll dp[19][48][2520];
int gcd(int a,int b){return b?gcd(b,a%b):a;}
ll dfs(int i,int preLcm,int preSum,bool e){
	if(i==-1)return preSum%preLcm==0;
	if(!e&&dp[i][b[preLcm]][preSum]!=-1)return dp[i][b[preLcm]][preSum];
	ll res=0;
	int u=e?a[i]:9,d;
	for(d=0;d<=u;d++){
		int nowLcm=preLcm,nowSum=(preSum*10+d)%LCM;
		if(d)nowLcm=preLcm*d/gcd(preLcm,d);
		res+=dfs(i-1,nowLcm,nowSum,e&&d==u);
	}
	return e?res:dp[i][b[preLcm]][preSum]=res;
}
ll cal(ll n){
	int i=0;
	while(n){a[i++]=n%10,n/=10;}
	return dfs(i-1,1,0,1);
}
int main(){
	int T,i,cnt=0;
	ll l,r;
	scanf("%d",&T);
	for(i=1;i<=LCM;i++)if(LCM%i==0)b[i]=cnt++;
	memset(dp,-1,sizeof(dp));
	while(T--){
		scanf("%I64d%I64d",&l,&r);
		printf("%I64d\n",cal(r)-cal(l-1));
	}
	return 0;
}
另外:有个优化,其实除了遍历到最后一位,之前的不用%2520,只需%252
#include<cstdio>
#include<cstring>
#define ll __int64
const int LCM=2520;
int a[19],b[LCM+1];
ll dp[19][48][252];
int gcd(int a,int b){return b?gcd(b,a%b):a;}
ll dfs(int i,int preLcm,int preSum,bool e){
	if(i==-1)return preSum%preLcm==0;
	if(!e&&dp[i][b[preLcm]][preSum]!=-1)return dp[i][b[preLcm]][preSum];
	ll res=0;
	int u=e?a[i]:9,d;
	for(d=0;d<=u;d++){
		int nowLcm=preLcm,nowSum=preSum*10+d;
		if(d)nowLcm=preLcm*d/gcd(preLcm,d);
		if(i)nowSum%=252;
		res+=dfs(i-1,nowLcm,nowSum,e&&d==u);
	}
	return e?res:dp[i][b[preLcm]][preSum]=res;
}
ll cal(ll n){
	int i=0;
	while(n){a[i++]=n%10,n/=10;}
	return dfs(i-1,1,0,1);
}
int main(){
	int T,i,cnt=0;
	ll l,r;
	scanf("%d",&T);
	for(i=1;i<=LCM;i++)if(LCM%i==0)b[i]=cnt++;
	memset(dp,-1,sizeof(dp));
	while(T--){
		scanf("%I64d%I64d",&l,&r);
		printf("%I64d\n",cal(r)-cal(l-1));
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值