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/ ...
随机推荐
- scala言语基础学习十二
- 【NOIP2007】矩阵取数
因为傻逼写错高精度搞了一下午浪费好多时间,好想哭qaq 原题: 帅帅经常更同学玩一个矩阵取数游戏:对于一个给定的n*m的矩阵,矩阵中的每个元素aij据为非负整数.游戏规则如下: 1. 每次取数时须从每 ...
- C# 控件缩写大全+命名规范+示例
如有转载,请注明出处:http://www.cnblogs.com/flydoos/archive/2011/08/29/2158903.html C# 控件缩写大全+命名规范+示例 写程序的时候突然 ...
- Clr core
http://hllvm.group.iteye.com/group/topic/43559
- JSBinding / Gen Bindings
Classes in JSBindingSettings.classes array will be exported to JavaScript. There are already many cl ...
- 有关<table>的几个问题
1)实现任意一行下边框的颜色设置: 单元格边距(表格填充)(cellpadding) -- 代表单元格外面的一个距离,用于隔开单元格与单元格空间 单元格间距(表格间距)(cellspacing) -- ...
- 自然语言处理3.3——使用Unicode进行文字处理
全世界有多种语言,经常需要应用程序处理不同的语言和字符集.下面将介绍如何利用Unicode处理使用非ASCII字符集文字. 1.什么是Unicode Unicode支持一百万种以上的字符,每一个字符分 ...
- sed命令拷屏
http://blog.sina.com.cn/s/blog_45497dfa0100w6r3.html sed样例较多,可以参考 http://blog.sina.com.cn/s/blog_6d ...
- shell算术运算与进制运算
(())与let是等效的 arithmetic expression type 与[是等效的 source与.是等效的 其实,Shell(这里是Bash)本身不具备处理浮点计算的能力,但是可以使用“b ...
- linux知识点
通过gui来使用通过api来使用通过cli来使用通过tui来使用 进程不在,但tcp连接还一直存在的解决办法--tcpkill命令 http://www.centoscn.com/CentOS/Int ...