CF 55D 数位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 isbeautiful 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 nextt 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 usecin (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 (fromli tori, inclusively).

Sample test(s)
Input
1
1 9
Output
9
Input
1
12 15
Output
2


题意:给定A,B,求A,B之间所有能被自己每一个非零位整除的数的个数。

乍一看毫无头绪,按直接的方法做,肯定会爆能存,看别人的思路,构造好巧妙,从1到9的最小公倍数是2520,每一个数被自己的非零位整除,则一定为2520的某一个因子整除,因此求和可以直接对2520取模,这样不影响结果,从1到2520,所有的最小公倍数只有48个,因此可以离散化一下,后面就是数位dp的过程了。dp[pos][presum][index[prelcm]]三个参数代表当前位的和,以及从前往后的lcm值,

代码:

/* ***********************************************
Author :xianxingwuguan
Created Time :2014-2-8 0:11:46
File Name :1.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
ll dp[30][3000][60],num[30],indexx[3000],cnt;
const ll mod=2520LL;
ll gcd(ll a,ll b){
	if(a==0)return b;
	return gcd(b%a,a);
}
ll lcm(ll a,ll b){
	return a/gcd(a,b)*b;
}
ll dfs(ll pos,ll presum,ll prelcm,bool flag){
	if(pos==0)return presum%prelcm==0;
	if(flag&&dp[pos][presum][indexx[prelcm]]!=-1)
		return dp[pos][presum][indexx[prelcm]];
	ll u=flag?9:num[pos];
	ll ans=0;
	for(ll d=0;d<=u;d++){
		ll newsum=(presum*10+d)%mod;
		ll newlcm=prelcm;
		if(d)newlcm=lcm(d,newlcm);
		ans+=dfs(pos-1,newsum,newlcm,flag||d<u);
	}
	if(flag)dp[pos][presum][indexx[prelcm]]=ans;
	return ans;
}
ll solve(ll x){
	ll len=0;
	while(x){
		num[++len]=x%10;
		x/=10;
	}
	//cout<<"hahah"<<endl;
	return dfs(len,0,1,0);
}
int main(){
	ll T,A,B;
	cnt=0;
	for(ll i=1;i<=mod;i++)
		if(mod%i==0)
			indexx[i]=cnt++;
	memset(dp,-1,sizeof(dp));
	 cin>>T;
	 while(T--){
		 cin>>A>>B;
		 //cout<<"hahah"<<endl;
		 cout<<solve(B)-solve(A-1)<<endl;
	 }

}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值