codeforces 55D - Beautiful numbers(数位DP+离散化)
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
题目链接:http://codeforces.com/problemset/problem/55/D
话说这是我第一次做codeforces,感觉他的提交好吊,就类似WF一样,看着好爽!!(我现在还不知道怎么在cf的题库里搜索题目,哪位大牛知道,指点一下,感激不尽!)
分析:一个数能被它的所有非零数位整除,则能被它们的最小公倍数整除,而1到9的最小公倍数为2520,数位DP时我们只需保存前面那些位的最小公倍数就可进行状态转移,到边界时就把所有位的lcm求出了,为了判断这个数能否被它的所有数位整除,我们还需要这个数的值,显然要记录值是不可能的,其实我们只需记录它对2520的模即可,这样我们就可以设计出如下数位DP:dfs(pos,mod,lcm,f),pos为当前位,mod为前面那些位对2520的模,lcm为前面那些数位的最小公倍数,f标记前面那些位是否达到上限,这样一来dp数组就要开到19*2520*2520,明显超内存了,考虑到最小公倍数是离散的,1-2520中可能是最小公倍数的其实只有48个,经过离散化处理后,dp数组的最后一维可以降到48,这样就不会超了。
分析链接:http://www.cnblogs.com/algorithms/archive/2012/09/02/2668021.html
题解:看了好几天,最后弄了个容易懂的代码(如上),看懂了,首先要把那个数对2520取模(因为这个数如果想要符合的话,首先要被2520整除,因为2520是1~9的lcm),这只是第一个条件(此时取模之后存在mod参数之中),第二个条件是对所有出现过的位数上的值取模,如果mod对位数上取模==0,就可以了。
- #include<cstdio>
- #include<cstring>
- #include<iostream>
- using namespace std;
- #define N 19
- #define MOD 2520
- typedef __int64 LL;
- int t[],cnt;
- LL dp[N][MOD][];
- int dig[N];
- 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()
- {
- cnt=;
- for (int i=;i<=MOD;i++)
- {
- if (!(MOD%i)) t[cnt++]=i;
- }
- memset(dp,-,sizeof(dp));
- }
- int Binary_search(int x)
- {
- int mid,l=,r=cnt;
- while(l<r-)
- {
- mid=l+r>>;
- if (t[mid]>x) r=mid;
- else l=mid;
- }
- return l;
- }
- LL dfs(int pos,int mod,int lcmid,bool f)
- {
- if (pos<) return !(mod%t[lcmid]);
- LL &aa=dp[pos][mod][lcmid];
- if (!f&&aa!=-) return aa;
- int end=f?dig[pos]:;
- LL res=;
- for (int i=;i<=end;i++)
- {
- int nmod=(mod*+i)%MOD;
- int nlcmid=lcmid;
- if (i) nlcmid=Binary_search(LCM(t[lcmid],i));
- res+=dfs(pos-,nmod,nlcmid,f&&i==end);
- }
- return f?res:aa=res;
- }
- LL sol(LL x)
- {
- int ind=;
- dig[]=;//这个一定不要少了,如果没有这个,1 9的时候会错
- for (;x;x/=) dig[ind++]=x%;
- dfs(ind-,,,);
- }
- int main()
- {
- int o;
- cin>>o;
- init();
- while(o--)
- {
- LL x,y;
- cin>>x>>y;
- cout<<sol(y)-sol(x-)<<endl;
- }
- return ;
- }
codeforces 55D - Beautiful numbers(数位DP+离散化)的更多相关文章
- 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是 ...
- FZU2179/Codeforces 55D beautiful number 数位DP
题目大意: 求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- 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的水题,觉得 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
随机推荐
- Python3字典中items()和python2.x中iteritems()有什么区别
在Python2.x中,items( )用于 返回一个字典的拷贝列表[Returns a copy of the list of all items (key/value pairs) in D],占 ...
- Windows Kernel Security Training Courses
http://www.codemachine.com/courses.html#kerdbg Windows Kernel Internals for Security Researchers Thi ...
- Python列表操作——模拟实现栈和队列
1.实现栈: stack=[] def pushit(): stack.append(raw_input('Enter New String:').strip()) def popit(): if l ...
- PHP快速排序及其时间复杂度
<?php function quickSort(&$arr, $l, $r) { if (count($arr)<2 || $l>$r) return; $tmp_l = ...
- Web前端开发笔试&面试_03
WL: 1.如何显示.隐藏一个dom对象? 2.如何将一个网页中的内容水平置中?写出重要的html标签和css. (css:#content{align:center;float:left;}html ...
- nedb nodejs 数据库学习
// Type 1: In-memory only datastore (no need to load the database) var Datastore = require('nedb') ...
- swagger-editor 快速REST-API 测试文档编写
1. 在线使用 http://editor.swagger.io/#/ 2. 离线工具 https://github.com/swagger-api/swagger-editor 3. 跨域访问问题: ...
- 现在流行什么 JS库/框架?
现在大家最感兴趣的 JS 库和框架是什么? jQuery 91.5% Underscore 38.6% AngularJS 28.5% Backbone 18.6% React 15.7% Knock ...
- 【spring】non-compatible bean definition of same name and class
org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected excep tion parsing XML do ...
- 【svn】 linux svn 强制提交注释
在svn版本库的hooks文件夹下面,复制模版pre-commit.tmpl cp pre-commit.tmpl pre-commit chmod +x pre-commit vi编辑,如下: #! ...