CF 55D
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.
Input
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
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).
Examples
1
1 9
9
1
12 15
2 数位dp。
在这道题中离散化是最关键的一步操作,不然dp数组占用内存过大,无法申请。
#include<cstdio>
#include<cstring> int shu[];
int lcm[];
long long dp[][][]; void init(){
memset(dp,-,sizeof(dp));
int cnt = ;
for(int i=;i<=;i++) // 离散化操作 将所有的因子处理出来
if(%i==)lcm[i]=++cnt;
//printf("%d\n",cnt); //
}
int gcd(int a, int b){
return b?gcd(b,a%b):a;
}
long long dfs(int k, int lc, int r, bool shangxian){
if(k==)return r%lc==;
if(!shangxian && dp[k][lcm[lc]][r]!=-)
return dp[k][lcm[lc]][r];
int maxn = shangxian?shu[k]:;
long long cnt = ;
for(int i=;i<=maxn;i++){
int now=lc;
if(i)
now *= i / gcd(i,lc);
cnt += dfs(k-,now,(r*+i)%,shangxian&&i==maxn);
}
return shangxian?cnt:dp[k][lcm[lc]][r]=cnt;
}
long long solve(long long x){
int k=;
while(x){
shu[++k] = x%;
x/=;
}
return dfs(k,,,);
}
int main(){
int t;
long long A, B;
init();
scanf("%d",&t);
while(t--){
scanf("%I64d %I64d",&A,&B);
printf("%I64d\n",solve(B)-solve(A-));
}
return ;
}
CF 55D的更多相关文章
- 数位DP CF 55D Beautiful numbers
题目链接 题意:定义"beautiful number"为一个数n能整除所有数位上非0的数字 分析:即n是数位所有数字的最小公倍数的倍数.LCM(1到9)=2520.n满足是252 ...
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- CF 55D - Beautiful numbers(数位DP)
题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...
- CF 55D Beautiful numbers (数位DP)
题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018 ...
- cf 55D 数位dp 好题
/* 刚开始我考虑0的情况,想将他剔除就将lcmn设为-1,这样还要判断0和lcmn是-1的情况很麻烦而且但是一直出错 后来觉得不用管0的情况就行了,可以认为符合. 解:将lcmn离散化,因为1-9的 ...
- 【数位dp】CF 55D Beautiful numbers
题目 Volodya is an odd boy and his taste is strange as well. It seems to him that a positive integer n ...
- CodeForces 55D Beautiful numbers (SPOJ JZPEXT 数位DP)
题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...
- 数位DP入门
HDU 2089 不要62 DESC: 问l, r范围内的没有4和相邻62的数有多少个. #include <stdio.h> #include <string.h> #inc ...
- 【转载】ACM总结——dp专辑
感谢博主—— http://blog.csdn.net/cc_again?viewmode=list ---------- Accagain 2014年5月15日 动态规划一 ...
随机推荐
- Java中关于String的比较
关于String的各种==和equals,有的人搞不懂 简而言之 String s1 = "Hello" 声明的是一个常量,会在常量池里. ...
- Android学习笔记_8_使用SharedPreferences存储数据
1.SharedPreferences介绍: Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPrefer ...
- DHTML---HTML5
1. HTML概述 网页是网站的表现层,各种编程语言(如Java)构成后台的逻辑,我们将后台逻辑做好然后通过页面表达.同时通过网页来与后台进行交互.而Html是我们做网页的基础,由浏览器来解析. 1. ...
- PThread 学习笔记
POSIX 线程,也被称为Pthreads,是一个线程的POSIX标准: pthread.h int pthread_create(pthread_t * thread, pthread_attr_t ...
- 大白话解释IP多播
多播引入 比方说我是一个班主任,管着三个班,每个班30个人,每个班有自己的班长.领导让我宣传19大视 频报告,我这里有源文件.对于普通的单播我需要复制90个副本,给各个班长每人30份,然后班 长在自己 ...
- iOS 语言国际化配置
所谓的iOS 国际化,就是根据系统不同的语言自动切换. 首先,如图: 创建一个.strings文件,以“Localizable.strings”为文件名: 创建完之后,在XCode的右边勾选自 ...
- zepto 基础知识(6)
101.$.ajax $.ajax(options) 类型:XMLttpRequest 执行Ajax请求.他可能是本地资源,或者通过支持HTTP access control的浏览器 或者通过 JSO ...
- 在C++中如何实现文件的读写
一.ASCII 输出为了使用下面的方法, 你必须包含头文件<fstream.h>(译者注:在标准C++中,已经使用<fstream>取代< fstream.h>,所 ...
- linux系统下病毒排除思路
1.top查看是否有特别吃cpu和内存的进程,病毒进程kill是杀不死的,因为ps命令被修改 2.ls -la /proc/病毒进程pid/ pwd为病毒进程程序目录 一般在/usr/bin下 3. ...
- sencha inspector(调试工具)
Sencha Inspector 一:安装sencha inspector 使用Sencha Inspector下载Ext JS试用版(可在此处获得). 下载后,双击下载的文件以启动安装程序,然后按照 ...