Codeforces Beta Round #51 D. 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
数位dp,这题,要求最后的数,对于,每一个数字都要整除,那么,我们就可以通过,一个数能整除所有数字的最小公倍数就可以了!所以我们用dp[i][j][]表示第i位,最小公倍数为j,第i位时余下的是k时的最后的个数,这题还可以进行优化,最是把最小公倍数编号,因为,2-9最小公倍数最大是2520,也就是说,只有48个,这样,大大节省了内存,加快速度!
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define M 23
__int64 dp[M][55][3500],pri[M];
int hash[3500],kk=0;
int gcd(int a,int b){
if(a==0)return b;
return gcd(b%a,a);
}
int get(int t,int i){
if(i==0||i==1)return t;
return t*i/gcd(t,i);
}
__int64 dfs(int pos,int t,int flag,int pre){
if(pos==0)return pre%t==0;
if(!flag&&dp[pos][hash[t]][pre]!=-1)return dp[pos][hash[t]][pre];
int u=flag?pri[pos]:9;
__int64 ans=0;
for(int i=0;i<=u;i++)
ans+=dfs(pos-1,get(t,i),flag&&i==u,(pre*10+i)%2520);
return flag?ans:dp[pos][hash[t]][pre]=ans;
}
__int64 solve(__int64 x){
int cnt=0;
while(x){
pri[++cnt]=x%10;x/=10;
}
return dfs(cnt,1,1,0);
}
int init(){
memset(dp,-1,sizeof(dp));
int cnt=0;
hash[0]=0;
for(int i=1;i<=8;i*=2)
for(int j=1;j<=9;j*=3)
for(int k=1;k<=5;k*=5)
for(int x=1;x<=7;x*=7)
hash[i*j*k*x]=++cnt;
}
int main()
{
int tcase;__int64 n,m;
init();
while(scanf("%d",&tcase)!=EOF){
while(tcase--){
scanf("%I64d%I64d",&m,&n);
printf("%I64d\n",solve(n)-solve(m-1));
}
}
return 0;
}
Codeforces Beta Round #51 D. Beautiful numbers的更多相关文章
- Codeforces Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/p ...
- Codeforces Beta Round #51 D. Beautiful numbers(数位dp)
题目链接:https://codeforces.com/contest/55/problem/D 题目大意:给你一段区间[l,r],要求这段区间中可以整除自己每一位(除0意外)上的数字的整数个数,例如 ...
- codeforces 55d//Beautiful numbers// Codeforces Beta Round #51
题意:一个数能整除它所有的位上的数字(除了0),统计这样数的个数. 注意离散化,为了速度更快需存入数组查找. 不要每次memset,记录下已有的长度下符合条件的个数. 数位dp肯定是从高位到低位. 记 ...
- Codeforces Beta Round #51 B. Smallest number dfs
B. Smallest number Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/pro ...
- Codeforces Beta Round #51 C. Pie or die 博弈论找规律 有趣的题~
C. Pie or die Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem/ ...
- Codeforces Beta Round #51 A. Flea travel 水题
A. Flea travel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem ...
- Codeforces Beta Round #80 (Div. 2 Only)【ABCD】
Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...
- Codeforces Beta Round #62 题解【ABCD】
Codeforces Beta Round #62 A Irrational problem 题意 f(x) = x mod p1 mod p2 mod p3 mod p4 问你[a,b]中有多少个数 ...
- Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】
Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...
随机推荐
- 使用PHP对文件进行压缩解压(zip)
使用虚拟主机进行文件上传时最常用的工具莫过于FTP了,但是使用FTP有一个弊端就是文件太多时上传或下载速度比较慢,如果上传时将文件打包,上传后在 空间解压缩,同样下载前将文件打包压缩以压缩包的形式下载 ...
- strtotime 的几点不同
在php里面,strtotime()有点比较特殊,date函数也有点问题 if ( date(1,time()) == "Monday") //似乎 date(1,time( ...
- oracle dataguard
startup mount restrict; drop database; alter database add standby logfile thread 1 group 8 '/oracle/ ...
- 在ios7真机上和iOS6模拟器上运行是好的,而在iOS6真机上运行却报错
在ios7真机上和iOS6模拟器上运行是好的,而在iOS6真机上运行却报错 解决方法: 或是都设置为yes.. Build Active Architecture Only的意思是只生成适应的指令集
- kafka java示例
http://www.open-open.com/lib/view/open1407942131801.html http://www.open-open.com/lib/view/open14079 ...
- 【 POJ - 3801】Crazy Circuits(有源汇、上下界最小流)
Description You’ve just built a circuit board for your new robot, and now you need to power it. Your ...
- 使用javascript获取网址的各个参数
有时也挺无奈的,为了实现一个功能,不得不用到前台获取参数.幸亏,有javascript,不然真的是坑大发了,感谢javascript的创造者.开始show大图: 属性 值href:完整的 URL ...
- C语言嵌入式系统编程修炼之六:性能优化
使用宏定义 在C语言中,宏是产生内嵌代码的唯一方法.对于嵌入式系统而言,为了能达到性能要求,宏是一种很好的代替函数的方法. 写一个"标准"宏MIN ,这个宏输入两个参数并返回较小的 ...
- FAT32,NTFS,EXT3,支持的最大分区和单个文件大小?
FAT32 Filesystem: 最大单一档案大小 4GB 最大文件系统总容量 128GB NTFS Filesystem: 最大单一档案大小 64GB 最大文件系统总容量 2TB Ext3 Fil ...
- 锐浪应用小插曲,asp.net下的使用
下午提前完成了今天的工作内容,整了下bs中的应用,嘿嘿,其中遇到不少问题,接下来说下大概会遇到哪些问题,1:grid++ 6.0插件下载安装之后ie浏览器无法打开,居然什么都没有显示,奇葩啊,系统版本 ...