CodeForces 55D Beautiful numbers
4 seconds
256 megabytes
standard input
standard output
Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits. We will not argue with this and just count the quantity of beautiful numbers in given ranges.
The first line of the input contains the number of cases t (1≤t≤10). Each of the next t lines contains two natural numbers li and ri (1≤li≤ri≤9·1018).
Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cin (also you may use %I64d).
Output should contain t numbers — answers to the queries, one number per line — quantities of beautiful numbers in given intervals (from li to ri, inclusively).
1
1 9
9
1
12 15
2
- /*
- 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}
- 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 = num++;
- }
- 很棒!!
- 所以复杂度大概为19*2520*48*10(状态数*决策数)
- 我觉得这题状态的设计不能跟具体数字分开,否则会很难设计吧
- 所以用记忆化搜索,存起来
- 用具体数字去计算,重复的子问题跟pre关系比较密切
- 有一个比较重要的切入点就是LCM,还有%MOD缩小范围,才能存储
- 还有优化到只需%252的,更快
- 不过我觉得%2520比较好理解
- */
|
#include <iostream>
#include <cstdio> #include <cstring> using namespace std; typedef long long int LL; const int MOD=2520; LL Gcd(LL a,LL b) LL Lcm(LL a,LL b) LL dp[20][MOD+10][50],lcm[MOD+10],bit[20]; void Init() LL dfs(int pos,int presum,int preLcm,bool limit) LL calu(LL x) int main() |
* This source code was highlighted by YcdoiT. ( style: Codeblocks )
CodeForces 55D Beautiful numbers的更多相关文章
- 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的水题,觉得 ...
- 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 D. Beautiful numbers time limit per test 4 seconds me ...
- 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 (SPOJ JZPEXT 数位DP)
题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...
- CodeForces 55D Beautiful numbers(数位dp+数学)
题目链接:http://codeforces.com/problemset/problem/55/D 题意:一个美丽数就是可以被它的每一位的数字整除的数. 给定一个区间,求美丽数的个数. 显然这是一道 ...
- CodeForces 55D Beautiful numbers(数位dp)
数位dp,三个状态,dp[i][j][k],i状态表示位数,j状态表示各个位上数的最小公倍数,k状态表示余数 其中j共有48种状态,最大的是2520,所以状态k最多有2520个状态. #include ...
- codeforces 55D. Beautiful numbers 数位dp
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
随机推荐
- Sender
多个对象用同一个方法的时候,想对多个对象分别操作的话就用Sender. BackGroundWorker worker1 = sender as BackGroundWork. 分别去取当前的对象 ...
- os和sys模块
sys模块 sys模块主要是用于提供对python解释器相关的操作 函数 sys.argv #命令行参数List,第一个元素是程序本身路径 sys.path #返回模块的搜索路径,初始化时使用PYTH ...
- ACM程序对拍
有时候在OJ刷题目的时候,总是会遇到不知名bug,题目总不能AC,自己测试的一些数据又都能得出正确的结果,又或是直接暴力会TLE,改了算法,但是仍然WA,这时候进行程序对拍测试数据不失为一个好办法.程 ...
- Exception:A generic error occurred in GDI+
分析: 一般出现这种问题都是GDI和原数据(比如Bitmap)是同一个实体,只不过是两个引用.换句话说就是这个路径的图片被GDI占用啦. 还有一种情况是路径有问题. 场景一: WPF的Image控件的 ...
- 常见linux命令释义(第七天)——ulimit 与变量内容的删除替代与替换。
linux是一个多用户多任务的系统,不同于windows的单人多任务操作系统.再linux上,在同一个时间点上,可以有多个人同时执行多个任务. 那么假若有10个用户,同时打开了100个100M的文件. ...
- HTML5 history
引入history.pushState的来龙去脉 大家都知道web2.0以来,大家都喜欢使用ajax来请求数据,提高用户体验,但是传统的ajax可以无刷新改变页面内容,但无法改变页面URL,无刷新的改 ...
- JAVA Socket超时浅析
JAVA Socket超时浅析 套接字或插座(socket)是一种软件形式的抽象,用于表达两台机器间一个连接的"终端".针对一个特定的连接,每台机器上都有一个"套接字&q ...
- git push to nas
1 建nas目录 在nas的/volume1/git_repos目录下新建相关的目录,并在该目录下运行git init --bare cd /volume1/git_repos mkdir wifi_ ...
- Linux下interface文件修改
我们来通过一些例子,来记录interfaces文件的书写.详情可参照man interfaces. 设置常用ethernet参数 auto lo iface lo inet loopback # Th ...
- Jasper(物联网网络支撑平台公司)的技术为什么这么牛逼?
Jasper在这个行业积累了十几年,合作的运营商超过30个,合作的行业大咖包括了通用.空客.宝马.特斯拉等几千个行业龙头,还是有很多积累下来的优势的. 一是,Jasper通过积累下来的行业应用经验,针 ...