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 ...
随机推荐
- 日期-用Datapicker实现前一天后一天
运用了JQuery UI Datepicker 插件和一些常用日期的方法.其中Datepicker的API具体可参考[http://api.jqueryui.com/datepicker/#optio ...
- PHP导出excel信息表格
//导出表格public function get_exel($fileName,$headArr,$list){//导入PHPExcel类库,因为PHPExcel没有用命名空间,只能import导入 ...
- php模块参考
<?php //数据库连接类 class ConnDB{ var $dbtype; var $host; var $user; var $pwd; var $dbname; //构造方法 fun ...
- Python3.4使用MySql
如何在Django1.6结合Python3.4版本中使用MySql django默认的mysql连接是Mysqldb,悲催的是此版本只支持到python2.7,oracle官方的mysql-conne ...
- 项目管理Point
1.项目管理流程 在设计阶段需要提交的成果物:类图设计(每个方法要有成形的SQL),页面设计,数据库设计,思路:需求分析->用户故事->客户确认(前三步是个迭代过程)->类图设计(了 ...
- MAC OX 配置JDK环境变量
大家在windows里面配置JDK环境变量很容易,但是如果要在mac里面配置JDK环境变量和windows里面有所不同,具体如下: 第一: mac OS里面自带jdk,不过是1.6的版本,现在很多人使 ...
- 前端开发福音!阿里Weex跨平台移动开发工具开源-b
阿里巴巴今天在Qcon大会上宣布跨平台移动开发工具Weex开放内测邀请.Weex能够完美兼顾性能与动态性,让移动开发者通过简捷的前端语法写出Native级别的性能体验,并支持iOS.安卓.YunOS及 ...
- 苹果搜索广告后台大揭秘,最全最细致详解,手把手设置教程「后附官方视频」-b
WWDC2016 搜索广告分会视频和 PPT 发布了,ASO100 带开发者第一时间了解 Search Ads 后台设置(文末有原声视频). 首先介绍一下搜索广告的模式和竞价规则 广告模式为 CPT( ...
- <meta http-equiv="pragma" content="no-cache"/>-备
<meta http-equiv="pragma" content="no-cache"/> 网页中常常看见有这样的标记,他们是清浏览器缓存用的啊, ...
- linux里的php使用phpize拓展各种功能(curl,zip,gd等等)
这里的实验以拓展zip功能为实例,成功使用zip功能需要如下步骤: 1.下载zip拓展包,并解压,并进入zip文件夹 tar -zxvf zip.tar.gz // 解压 cd zip //进入文件夹 ...