codeforces 55D Beautiful numbers

本文探讨如何利用数位DP技巧解决一个区间内BeautifulNumbers的数量计算问题。BeautifulNumbers定义为能被其所有非零位数字整除的正整数。通过构造递归函数并优化计算过程,有效减少搜索范围,实现快速计数。
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;
}


### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值