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日 动态规划一 ...
随机推荐
- POJ 2533 Longest Ordered Subsequence(裸LIS)
传送门: http://poj.org/problem?id=2533 Longest Ordered Subsequence Time Limit: 2000MS Memory Limit: 6 ...
- Java实现桶排序
public class BucketSort { public static void main(String[] args) { int[] list = {1000, 192, 221, 12, ...
- CocoaPods介绍与使用(转)
一.介绍 (1) CocoaPods是一个用来帮助我们管理第三方依赖库的工具.它可以解决库与库之间的依赖关系,下载库的源代码,同时通过创建一个Xcode的workspace来将这些第三方库和我们的工程 ...
- N个数求和
题目: 本题的要求很简单,就是求N个数字的和.麻烦的是,这些数字是以有理数分子/分母的形式给出的,你输出的和也必须是有理数的形式. 输入格式: 输入第一行给出一个正整数N(≤100).随后一行按格式a ...
- Ganglia监控安装配置
172.17.20.123 node1 gmetad.gmond.web 172.17.20.124 node2 gmond 1.服务器安装好epel源后,安装ganglia yum install ...
- linux系统之-vi编辑器
在linux系统使用中,掌握熟练的vi编辑器,可以提高linux工作效率.那么vi编辑器的使用方法有哪些呢? vi编辑器可在绝大部分linux发行版中使用. Vi编辑器的作用:创建或修改文件:维护li ...
- HTML基础全荟
第一讲 html概述 1.认识HTML <! DOCTYPE html> <html> <style></style> <head>< ...
- css3新样式
超出两行变省略号 overflow:hidden; text-overflow:ellipsis;display:-webkit-box; -webkit-box-orient:vertical;-w ...
- Git 项目提交代码及一些常用命令
在dev_ysg分支 : git add . //把项目添加到仓库 git commit -m "test" // 提交加注释 git push //推到dev_ysg分支上去 g ...
- web3.js_1.x.x--API(二)/合约部署与事件调用
web3.js_1.x.x的使用和网上查到的官方文档有些不同,我对经常使用到的API进行一些整理,希望能帮到大家 转载博客:http://www.cnblogs.com/baizx/p/7474774 ...