Description

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).

Sample Input

Input
1
1 9
Output
9
Input
1
12 15
Output
2

让你找[l,r]区间中,能被自己各个非零数位整除的数的个数。一看就是满足区间减法。现在就讨论怎么求就行了。
首先lcm(1..9)=2520, int MOD=2520;保存每一个数是不现实的,所以我们就.保存x%MOD就行了。
preSum表示已经讨论的前面的几个数位的值(前串),preLcm表示前穿的Lcm。
这里注意到1...9的各种lcm可以离散化处理,只有48个,这样可以大大减少数组的空间。
我们再用flag来表示当前位的数字大小是否超过x对应位的大小
例:x=15666;当我们讨论到千位是1,2,3,4时,后面三位是随便选的,讨论千位是5是,百位就不能随便选了,要<=6,此时在千位我们就达到了边界。
剩下的交给dfs。
PS:有人把2520优化成252的,92ms过了...我1122ms...
代码如下:
 #include <bits/stdc++.h>

 using namespace std;

 const int MAXN=;
const int MOD=;
long long dp[MAXN][MOD][];//dp[i][j][k]表示处理到第i位,前串数(取模后)是j,前串树lcm是k时,后面位随便变的合法情况的个数
int index[MOD+],bit[MAXN];//index表示1..9的各种组合lcm,bit是将数字的每一位拆开保存
long long int gcd (long long int a,long long int b) {return (b==)?a:gcd(b,a%b);}
long long int lcm (long long int a,long long int b){return a/gcd(a,b)*b;}
void init()//来找1...9之间各种组合的lcm
{
int num=;
for (int i=;i<=MOD;++i)
if (MOD%i==)
index[i]=num++;
}
long long dfs (int pos,int preSum,int preLcm,bool flag)//pos当前位,flag前面几位是否达到边界
{
if (pos==-)//讨论到最后一位
return preSum%preLcm==;//如果这个数满足要求,+1
if (!flag && dp[pos][preSum][index[preLcm]]!=-)//没达到边界而且访问过这个状态
return dp[pos][preSum][index[preLcm]];//直接return,记忆化搜索
long long ans=;
int endd=flag?bit[pos]:;//这位达到边界时,下一位从0到x的对应位变化。没达到边界是0...9变化
for (int i=;i<=endd;i++)
{
int nowSum=(preSum*+i)%MOD;//添加下一位数字,然后更新状态
int nowLcm=preLcm;
if (i)
nowLcm=lcm(nowLcm,i);
ans+=dfs(pos-,nowSum,nowLcm,flag&&i==endd);
}
if (!flag)
dp[pos][preSum][index[preLcm]]=ans;
return ans;
}
long long calc (long long x)
{
memset(bit,,sizeof bit);
int pos=;
while (x)
{
bit[pos++]=x%;
x/=;
}
return dfs(pos-,,,);
}
int main()
{
int t;
long long int l,r;
init();
memset(dp,-,sizeof dp);
scanf("%d",&t);
while (t--)
{
scanf("%I64d%I64d",&l,&r);
printf("%I64d\n",calc(r)-calc(l-));
}
return ;
}
 

Codeforces #55D (数位dp+离散化)的更多相关文章

  1. Codeforces 55D (数位DP+离散化+数论)

    题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...

  2. codeforces 55D 数位dp

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  3. CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)

    传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...

  4. codeforces 55D - Beautiful numbers(数位DP+离散化)

    D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...

  5. Codeforces 628D 数位dp

    题意:d magic number(0<=d<9)的意思就是一个数,从最高位开始奇数位不是d,偶数位是d 题目问,给a,b,m,d(a<=b,m<2000)问,a,b之间有多少 ...

  6. codeforces 401D (数位DP)

    思路:很明显的数位dp,设dp[i][j] 表示选取数字的状态为i,模m等于j的数的个数,那么最后的答案就是dp[(1<<n)-1][0].状态转移方程就是,dp[i|(1<< ...

  7. Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)

    大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为1 ...

  8. Codeforces - 914C 数位DP

    题意有点难以描述,简略的就是给定一个二进制\(n\),每一步操作能使\(n\)的位为1的数的和转化为一个十进制,然后转化为该数的二进制再进行相同的操作 查询\([0,n]\)中操作数恰好为\(k\)的 ...

  9. Codeforces 13C Sequence --DP+离散化

    题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为 ...

随机推荐

  1. Apache搭建http网站服务器入门教程

    Apache搭建http网站服务器入门教程 准备工具 一台带有Linux系统的主机,这里使用CentOS 7.1 64位系统 一个备案过的域名,这里使用www.hellopage.cn 一台可以访问网 ...

  2. 数据挖掘:周期性分析SMCA算法

    数据挖掘:周期性分析SMCA算法 原文地址:http://ieeexplore.ieee.org/stamp/stamp.jsp?arnumber=1423978 算法介绍 以时间顺序挖掘周期性的模式 ...

  3. [CSP-S模拟测试]:壕游戏(费用流)

    题目传送门(内部题18) 输入格式 第一行包括四个数$n,m,k,s$表示有$n$个剧情点,$m$个关卡,要玩$k$次游戏,$s$个完结点接下来一行包含$s$个数,代表$s$个完结点的编号.接下来$m ...

  4. python类与对象练习题扑克牌

    #定义一个扑克类,属性是颜色,数字.#定义一个手类,属性是扑克牌得颜色数字#定义一个人类,属性是左手,右手.类里定义一些方法,比如交换,展示 class Poker : def __init__(se ...

  5. SQL学习记录:函数(二)

    字符串函数 1.获取字符的ASCII码 语法结构: ASCII(espression)    这里的expression是一个返回char或varchar数据类型的表达式,ASCII函数仅对表达式最左 ...

  6. php面试专题---4、流程控制考点

    php面试专题---4.流程控制考点 一.总结 一句话总结: 理解循环内部机制(指针操作),更易于记忆foreach的reset特性,分支结构中理解了switch...case的执行步骤(跳转表)也就 ...

  7. webpack 4.+版本需要注意的地方

    在webpack打包的时候需要npm下载webpack-cli,而且打包JS的命令从以前的webpack .\src\main.js  .\dist\boundle.js 要编程 webpack .\ ...

  8. 如何在Oracle中建表空间、建用户并导入dmp文件详解

    假设oracle有个全新的数据库orcl,现在要把数据库文件(.dmp)导入这个全新的数据库orcl中.详细步骤如下:    1. 创建表空间  例如:  create tablespace test ...

  9. Java学习之线程通信(多线程(Lock))--生产者消费者

    SDK1.5版本以后对synchronized的升级,其思想是:将同步和锁封装成对象,其对象中包含操作锁的动作. 代码: //1.导入包 import java.util.concurrent.loc ...

  10. Maven Could not open ServletContext resource [/WEB-INF/applicationContext.xml]

    因此:maven中,配置文件的读取应有下列红字两部分,缺一不可. 不是maven项目时,可以都不写........ <context-param> <param-name>co ...