Codeforces 55D Beautiful Number (数位统计)

本文介绍了一种使用记忆化搜索优化数位DP的方法,通过计算特定区间内美丽数的数量,展示了如何利用LCM和模运算来减少状态数量,从而简化代码并提高效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多。

下面为转载内容:

   a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits.
    问一个区间内[l,r]有多少个Beautiful数字
    范围9*10^18
    
    数位统计问题,构造状态也挺难的,我想不出,我的思维局限在用递推去初始化状态,而这里的状态定义也比较难
    跟pre的具体数字有关

    问了NotOnlySuccess的,豁然开朗  Orz
    
    一个数字要被它的所有非零位整除,即被他们的LCM整除,可以存已有数字的Mask,但更好的方法是存它们的LCM{digit[i]}
    int MOD = LCM{1,2,9} = 5 * 7 * 8 * 9 = 2520
    按照定义,数字x为Beautiful : 
    x % LCM{digit[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
    而next,nextLcm是变量,上面的比较式的意义就是
    在已知pos , preSum , preLcm情况下有多少种(next,nextLcm)满足式子为0
    而这个就是一个重复子问题所在的地方了,需要记录下来,用记忆化搜索
    dfs(pos , preSum , preLcm , doing)
    加一个标记为doing表示目前是在计算给定数字的上限,还是没有上限,即***类型的
    这样就将初始化以及逐位统计写在一个dfs了,好神奇!!!
    
    还有一点,10以内的数字情况为2^3 , 3^2 , 5 , 7
    所以最小公倍数组合的情况只有4*3*2*2 = 48
    可以存起来,我看NotOnlySuccess的写法是
    for(int i = 1 ; i <= MOD ; i ++)
    {
        if(MOD % i == 0)
            index[i] = num++;
    }
    很棒!!

    所以复杂度大概为19*2520*48*10(状态数*决策数)

    我觉得这题状态的设计不能跟具体数字分开,否则会很难设计吧
    所以用记忆化搜索,存起来
    用具体数字去计算,重复的子问题跟pre关系比较密切
    有一个比较重要的切入点就是LCM,还有%MOD缩小范围,才能存储

    还有优化到只需%252的,更快
    不过我觉得%2520比较好理解

代码:

 1 const int MOD = 2520;
 2 
 3 LL dp[21][MOD][50];
 4 int digit[21];
 5 int indx[MOD+5];
 6 
 7 void init() {
 8     int num = 0;
 9     for(int i = 1; i <= MOD; ++i) {
10         if(MOD%i == 0) indx[i] = num++;
11     }
12     CL(dp, -1);
13 }
14 
15 LL gcd(LL a, LL b) {
16     return b == 0 ? a : gcd(b, a%b);
17 }
18 
19 LL lcm(LL a, LL b) {
20     return a/gcd(a, b)*b;
21 }
22 
23 LL dfs(int pos, int presum, int prelcm, bool edge) {
24     if(pos == -1)    return presum%prelcm == 0;
25     if(!edge && dp[pos][presum][indx[prelcm]] != -1)
26         return dp[pos][presum][indx[prelcm]];
27     int ed = edge ? digit[pos] : 9;
28     LL ans = 0;
29     for(int i = 0; i <= ed; ++i) {
30         int nowlcm = prelcm;
31         int nowsum = (presum*10 + i)%MOD;
32         if(i)   nowlcm = lcm(prelcm, i);
33         ans += dfs(pos - 1, nowsum, nowlcm, edge && i == ed);
34     }
35     if(!edge)    dp[pos][presum][indx[prelcm]] = ans;
36     return ans;
37 }
38 
39 LL cal(LL x) {
40     CL(digit, 0);
41     int pos = 0;
42     while(x) {
43         digit[pos++] = x%10;
44         x /= 10;
45     }
46     return dfs(pos - 1, 0, 1, 1);
47 }
48 
49 int main() {
50     //Read();
51 
52     init();
53     int T;
54     LL a, b;
55     cin >> T;
56     while(T--) {
57         cin >> a >> b;
58         cout << cal(b) - cal(a - 1) << endl;
59     }
60     return 0;
61 }
View Code

 

### 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?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值