Codeforces 55D Beautiful numbers 数位DP

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).

Examples
input
1
1 9
output
9
input
1
12 15
output
2


比较复杂的一题数位DP。

要求所求数被它的所有非零数位整除。


可以记录当前所有非零数位的最小公倍数,这个数字最大为5*7*8*9=2520.由此,当前DP到的数字可以取余,映射到0到2519。0到2520当中,可以作为最小公倍数的只能是2520的因子,最多只有50个左右。把这些数离散化,用dp[len][hash[mod]][sum%mod]进行记忆化搜索。

#include <cstdio>
#include <iostream>
#include <string.h>
#include <string> 
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
#include <math.h>
#include <cmath>
#include <bitset>
#define mem0(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,0x3f,sizeof(a))
using namespace std;
typedef long long ll;
typedef long double ld;
const int inf=0x3f3f3f3f,mod=2520;
const ll llinf=0x3f3f3f3f3f3f3f3f; 
ll dp[20][55][mod+5];
int num[25];
int hash[mod+5];

int gcd(int x,int y) {
	int a,b,c;
	a=x,b=y;
	while (b) {
		c=b;
		b=a%b;
		a=c;
	}
	return a;
}

int lcm (int x,int y) {
	return x*y/gcd(x,y);
}

ll dfs(int len,int nlcm,int sum,bool HaveLimit) {
	if (!len) return sum%nlcm==0;
	if (!HaveLimit&&dp[len][hash[nlcm]][sum]!=-1) 
	    return dp[len][hash[nlcm]][sum];
	    
	int p; 
	if (HaveLimit) p=num[len]; else p=9;
	ll ans=0;
	for (int i=0;i<=p;i++) {
		int ne;
		if (i!=0) ne=lcm(nlcm,i); else ne=nlcm;
		ans+=dfs(len-1,ne,(sum*10+i)%mod,i==num[len]?HaveLimit:0);
	}
	if (!HaveLimit) dp[len][hash[nlcm]][sum]=ans;
	return ans;
}

ll solve(ll n) {
	if (n==0) return 1;
	int i=0;
	ll k=n;
	while (k) {
		num[++i]=k%10;
		k/=10;
	}
	return dfs(i,1,0,1);
}

int main() {
	int cas,m=0;
	ll l,r;
	scanf("%d",&cas);
	memset(dp,-1,sizeof(dp));
	for (int i=1;i<=2520;i++) {
		if (mod%i==0) hash[i]=++m;
	}
	while (cas--) {
		scanf("%I64d%I64d",&l,&r);
		ll ans=solve(r)-solve(l-1);
		printf("%I64d\n",ans);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值