把数位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比较好理解

代码:

 const int MOD = ;

 LL dp[][MOD][];
int digit[];
int indx[MOD+]; void init() {
int num = ;
for(int i = ; i <= MOD; ++i) {
if(MOD%i == ) indx[i] = num++;
}
CL(dp, -);
} LL gcd(LL a, LL b) {
return b == ? a : gcd(b, a%b);
} LL lcm(LL a, LL b) {
return a/gcd(a, b)*b;
} LL dfs(int pos, int presum, int prelcm, bool edge) {
if(pos == -) return presum%prelcm == ;
if(!edge && dp[pos][presum][indx[prelcm]] != -)
return dp[pos][presum][indx[prelcm]];
int ed = edge ? digit[pos] : ;
LL ans = ;
for(int i = ; i <= ed; ++i) {
int nowlcm = prelcm;
int nowsum = (presum* + i)%MOD;
if(i) nowlcm = lcm(prelcm, i);
ans += dfs(pos - , nowsum, nowlcm, edge && i == ed);
}
if(!edge) dp[pos][presum][indx[prelcm]] = ans;
return ans;
} LL cal(LL x) {
CL(digit, );
int pos = ;
while(x) {
digit[pos++] = x%;
x /= ;
}
return dfs(pos - , , , );
} int main() {
//Read(); init();
int T;
LL a, b;
cin >> T;
while(T--) {
cin >> a >> b;
cout << cal(b) - cal(a - ) << endl;
}
return ;
}

Codeforces 55D Beautiful Number (数位统计)的更多相关文章

  1. FZU2179/Codeforces 55D beautiful number 数位DP

    题目大意: 求  1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...

  2. Codeforces 55D Beautiful Number

    Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...

  3. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  4. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

  5. CodeForces - 55D Beautiful numbers —— 数位DP

    题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...

  6. Codeforces - 55D Beautiful numbers (数位dp+数论)

    题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...

  7. codeforces 55D. Beautiful numbers 数位dp

    题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...

  8. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  9. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

随机推荐

  1. Xamarin开发Android笔记:TextView行间距设定

    TextView 在使用TextView的时候会遇到调整行间距的问题,可通过Layout文件添加属性完成,具体属性如下: //设置行间距,如”3dp”. android:lineSpacingExtr ...

  2. AngularJS实战项目(Ⅰ)--含源码

    前言 钻研ABP框架的日子,遇到了很多新的知识,因为对自己而言是新知识,所以经常卡在很多地方,迟迟不能有所突破,作为一个稍有上进心的程序员,内心绝对是不服输的,也绝对是不畏困难的,心底必然有这样一股力 ...

  3. js promise 风格编程

    使用q 这种方式,极大的避免了回调地狱的情况产生,以后打算长久用这种方式. 再写Nodejs,再也不担心这个问题了. 以下实例,作为连接数据库的公共方法. /** * Created by Think ...

  4. [游戏模版20] Win32 物理引擎 加速运动

    >_<:Compared with previous talk,there will be taking about how to create an accelerated speed. ...

  5. Web API 简单示例

    一.RESTful和Web API Representational State Transfer (REST) is a software architecture style consisting ...

  6. Gradle的属性设置大全

    Gradle作为一款项目构建工具,由于其强大.灵活.快速.完全兼容Ant等特性,越来越受到人们欢迎.Gradle的灵活有时候也会引起人们的困惑.比如在Gradle中设置变量和属性就有N种办法.由于Gr ...

  7. java 32位MD5加密的大写字符串

    package com.aok.test; import java.security.MessageDigest; public class MD5Test { public static void ...

  8. CSS 冲击波(水波纹)效果

    <span style="font-size:18px;"><!DOCTYPE html> <html> <head> <me ...

  9. Java-认识字符集-转载

    问题起源 对于计算机而言,它仅认识两个0和1,不管是在内存中还是外部存储设备上,我们所看到的文字.图片.视频等等“数据”在计算机中都是已二进制形式存在的.不同字符对应二进制数的规则,就是字符的编码.字 ...

  10. paip.web数据绑定 下拉框的api设计 选择框 uapi python .net java swing jsf总结

    paip.web数据绑定 下拉框的api设计 选择框 uapi  python .net java swing jsf总结 ====总结: 数据绑定下拉框,Uapi 1.最好的是默认绑定..Map(k ...