D. Beautiful numbers
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard outputVolodya 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
分析:套用数位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;
}
本文探讨如何利用数位DP技巧解决一个区间内BeautifulNumbers的数量计算问题。BeautifulNumbers定义为能被其所有非零位数字整除的正整数。通过构造递归函数并优化计算过程,有效减少搜索范围,实现快速计数。
749

被折叠的 条评论
为什么被折叠?



