Codeforces 55D Beautiful Number (数位统计)
把数位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 (数位统计)的更多相关文章
- FZU2179/Codeforces 55D beautiful number 数位DP
题目大意: 求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...
- Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number a positive integer number is beautiful if and only if it is divisibl ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- codeforces 55D. Beautiful numbers 数位dp
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
随机推荐
- Html5 学习系列(六)Html5本地存储和本地数据库
一个网站如何能在客户的浏览器存储更多的数据呢? 在Html4的时代在浏览器端存储点网站个性化的数据,尤其是用户浏览器的痕迹,用户的相关数据等一般只能存储在Cookie中,但是大多是浏览器对于Cooki ...
- Spring-Context之一:一个简单的例子
很久之前就想系统的学习和掌握Spring框架,但是拖了很久都没有行动.现在趁着在外出差杂事不多,就花时间来由浅入深的研究下Spring框架.Spring框架这几年来已经发展成为一个巨无霸产品.从最初的 ...
- javascript 中关于对象转换数字值的一些特点
下面是摘至<Javascript 高级程序设计第三版>里的一段话 是关于对象转换数字值的一些规则 "在应用于对象时,先调用对象的valueOf()方法以取得一个可供操作的值.然后 ...
- 从题目中学习java语法
一.输入输出 1.输入圆的半径,计算并输出圆的周长和面积: import java.util.Scanner; public class zuoye01_circle { public static ...
- iOS7初体验(3)——图像资源Images Assets
开始之前,首先回顾一下iOS7初体验(1)——第一个应用程序HelloWorld中的一张图,如下所示: 本文便分享一下Images.xcassets的体验~_~ 1. 打开此前使用过的HelloWor ...
- Autoprefixer处理CSS3属性前缀
http://www.w3cplus.com/css3/autoprefixer-css-vender-prefixes.html
- MySQL基础操作(一)
1. 连接数据库mysql -h localhost -u root -p 123456 2. 查看数据库版本select version(); 3. 查看数据库支持的存储引擎和当前默认存储引擎sho ...
- MongoDB学习笔记——索引管理
索引 索引能够提升查询的效率.没有索引,MongoDB必须扫描集合中的所有文档,才能找到匹配查询语句的文档. 索引是一种特殊的数据结构,将一小块数据集保存为容易遍历的形式.索引能够存储某种特殊字段或字 ...
- SQL Server 内存中OLTP内部机制概述(一)
----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<SQL Server In-Memory ...
- innosetup安装之前关闭进程
InnoSetup覆盖安装的时候可能会因为源程序正在运行而安装失败,以下脚本能够关闭原运行进程. [code] // 安装前检查关闭**进程 function InitializeSetup():Bo ...