把数位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. UML中依赖(Dependency)和关联(Association)之间的区别

    一般情况下,使用关联(association)来表示像类中的字段等.这个关系是始终存在的,因此你可以随时针对关联项进行访问调用,例如可以始终从 Customer 对象获取 Order 对象.但事实上它 ...

  2. GoogleApis 屏蔽

    . 需要maven包,但是官方库里面,引用了googleapis的一些文件.因为gfw,慢的要死,拖垮了整个页面. 查了一下,在hosts修改googleapis解析时候的ip就好了: 把Google ...

  3. 转:DataSet、DataTable、DataRow、DataColumn区别及使用实例

    DataSet 表示数据在内存中的缓存. 属性 Tables  获取包含在 DataSet 中的表的集合. ds.Tables["sjxx"] DataTable 表示内存中数据的 ...

  4. Linux系列笔记 - vim相关记录

    一.常用到的vim命令 这里只简单记录常用到的命令,后面会有自己记录的命令,但有些可能不常用. 常规模式: gg 跳到文件头 shift+g 跳到文件尾 行数+gg 跳到指定行 如:123gg 跳到1 ...

  5. Atitit。如何实现dip, di ,ioc ,Service Locator的区别于联系

    Atitit.如何实现dip, di ,ioc  ,Service Locator的区别于联系 1. Dip原则又来自于松耦合思想方向1 2. 要实现dip原则,有以下俩个模式1 3. Ioc和di的 ...

  6. iOS开发——高级技术&调用地图功能的实现

    调用地图功能的实现 一:苹果自带地图 学习如逆水行舟,不进则退.古人告诉我们要不断的反思和总结,日思则日精,月思则月精,年思则年精.只有不断的尝试和总结,才能让我们的工作和生活更加 轻松愉快和美好.连 ...

  7. Leetcode 172 Factorial Trailing Zeroes

    给定一个数n 求出n!的末尾0的个数. n!的末尾0产生的原因其实是n! = x * 10^m 如果能将n!是2和5相乘,那么只要统计n!约数5的个数. class Solution { public ...

  8. Ubuntu 安装JDK步骤 ,提示没有那个文件或目录

    作为一个程序员,配置环境是最基本的功夫,然而我却捣鼓了一下午,包括安装Ubuntu,安装JDK和配置环境变量. 简单记录下自己的安装过程: 1  下载JDK tar包,使用tar -xzvf jdk* ...

  9. python matplotlib

    背景: 1)数据可视化 目前还处于python入门阶段,主要想通过numpy.matplotlib进行数据可视化. 安装: 操作系统:windows7 1)python2.7 安装numpy.matp ...

  10. Windows KB2984972安装后堵住了一个windows 7 桌面可以多个用户远程访问桌面的漏洞。

    之前网络上有方法可以实现2个用户同时使用一个windows 7,一个在终端,一个通过远程桌面. 安装了这个kb后,就无法同时登陆了,同一时间只有一个用户可以登陆windows 7