题目链接

题意:定义"beautiful number"为一个数n能整除所有数位上非0的数字

分析:即n是数位所有数字的最小公倍数的倍数。LCM(1到9)=2520。n满足是2520的约数的倍数。dp[len][val][lcm]一维为数的位数,一维为%2520的值(保存原数不可能,也没必要,2520是可行的最小公倍数最大的一个),一维为当前数位的lcm,判断满足的条件是val%lcm==0。这题离散化2520的约数,否则空间开不下。

#include <bits/stdc++.h>

typedef long long ll;
ll dp[20][2520][50];
int digit[20];
int id[2521];
int tot; int GCD(int a, int b) {
return b ? GCD (b, a % b) : a;
}
int LCM(int a, int b) {
return a / GCD (a, b) * b;
} void init_LCM() {
tot = 0;
for (int i=1; i<=2520; ++i) {
if (2520 % i == 0) {
id[i] = ++tot;
}
}
} ll DFS(int pos, int val, int lcm, bool limit) {
if (pos == -1) {
return val % lcm == 0;
}
ll &now = dp[pos][val][id[lcm]];
if (!limit && now != -1) {
return now;
}
int d = limit ? digit[pos] : 9;
ll ret = 0;
for (int i=0; i<=d; ++i) {
int tval = (val * 10 + i) % 2520;
int tlcm = i ? lcm / GCD (lcm, i) * i : lcm;
ret += DFS (pos - 1, tval, tlcm, limit && i == d);
}
if (!limit) {
now = ret;
}
return ret;
} ll solve(ll x) {
int len = 0;
if (x == 0) {
digit[len++] = 0;
} else {
while (x) {
digit[len++] = x % 10;
x /= 10;
}
}
return DFS (len - 1, 0, 1, true);
} int main() {
init_LCM ();
memset (dp, -1, sizeof (dp));
int T; scanf ("%d", &T);
while (T--) {
ll l, r; std::cin >> l >> r;
std::cout << solve (r) - solve (l - 1) << '\n';
}
return 0;
}

  

数位DP CF 55D Beautiful numbers的更多相关文章

  1. 【数位dp】CF 55D Beautiful numbers

    题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...

  2. CF 55D - Beautiful numbers(数位DP)

    题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...

  3. CF 55D Beautiful numbers (数位DP)

    题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018 ...

  4. CF 55D. Beautiful numbers(数位DP)

    题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...

  5. 【数位DP】CF55D Beautiful numbers

    $dp[x][p][pp]$表示第x位,当前已有数字mod 2520(1~9数字的lcm)为p,当前各位数字的lcm为pp 观察到数组太大,考虑压缩,第三维lcm最多只有9个数字,打表发现最多只有48 ...

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

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

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

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

  8. [Codeforces-div.1 55D] Beautiful numbers

    [Codeforces-div.1 55D] Beautiful numbers 试题分析 还是离散化...\(f_{i,j,k}\)表示i位,gcd为j,余数为k. #include<iost ...

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

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

随机推荐

  1. Web Service 中返回DataSet结果大小改进

    http://www.cnblogs.com/scottckt/archive/2012/11/10/2764496.html Web Service 中返回DataSet结果方法: 1)直接返回Da ...

  2. easyUI 中datagrid 返回列隐藏方法

    easyui的datagrid方法返回的列,有的值不需要显示可以使用hidden(属性进行隐藏) columns : [ [{ field : 'bailClass', title : '类别', w ...

  3. 用U盘安装系统的好用的PE系统:通用PE V6.1下载

    用U盘安装系统的好用的PE系统:通用PE V6.1下载 PE是一款用其他介质(我们最常用的是U盘)启动安装电脑系统的简易操作系统,在XP系统中 最经典的是扬州老毛桃出品的只有100多兆的XP内核的PE ...

  4. win10如何100%提升网络速度

    1.Win+R运行,输入gpedit.msc 2.依次打开"计算机配置"-"管理模板"-"网络"-"QoS数据包计划程序" ...

  5. wordpress 函数、条件判断以及文件的总结

    WordPress基本模板文件 一套完整的WordPress模板应至少具有如下文件: style.css : CSS(样式表)文件 index.php : 主页模板 archive.php : Arc ...

  6. 在写junit test 的时候出现的java.lang.UnsupportedClassVersionError问题

    今天在写为一些project 写 一些junit  test  的时候,出现下面的异常: java.lang.UnsupportedClassVersionError: moneytest/Money ...

  7. 二叉树的层序遍历 BFS

    二叉树的层序遍历,或者说是宽度优先便利,是经常考察的内容. 问题一:层序遍历二叉树并输出,直接输出结果即可,输出格式为一行. #include <iostream> #include &l ...

  8. 15个JavaScript本地存储技术的函数库和工具

    当构建更复杂的JavaScript应用程序运行在用户的浏览器是非常有用的,它可以在浏览器中存储信息,这样的信息可以被共享在不同的页面,浏览会话. 在最近的过去,这将有可能只被cookies文本文件保存 ...

  9. jvm死锁解决

    那我们怎么确定一定是死锁呢?有两种方法. 1>使用JDK给我们的的工具JConsole,可以通过打开cmd然后输入jconsole打开. 1)连接到需要查看的进程.

  10. 第3月第13天 cpp模版 Iterator模式 proactor

    1.模版除了传参,还可以自动创建.而传指针只是传参而已. template <class TYPE, class FUNCTOR, class ACE_LOCK, typename TIME_P ...