D. Beautiful numbers
time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

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.

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 ≤ liri ≤ 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 test(s)
input
1
1 9
output
9
input
1
12 15
output
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的更多相关文章

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

  2. Codeforces Beta Round #51 D. Beautiful numbers(数位dp)

    题目链接:https://codeforces.com/contest/55/problem/D 题目大意:给你一段区间[l,r],要求这段区间中可以整除自己每一位(除0意外)上的数字的整数个数,例如 ...

  3. codeforces 55d//Beautiful numbers// Codeforces Beta Round #51

    题意:一个数能整除它所有的位上的数字(除了0),统计这样数的个数. 注意离散化,为了速度更快需存入数组查找. 不要每次memset,记录下已有的长度下符合条件的个数. 数位dp肯定是从高位到低位. 记 ...

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

  5. 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/ ...

  6. Codeforces Beta Round #51 A. Flea travel 水题

    A. Flea travel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem ...

  7. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  8. 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]中有多少个数 ...

  9. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

随机推荐

  1. HierarchyView的实现原理和Android设备无法使用HierarchyView的解决方法

    声明:由于本人一直用eng版的真机调试,所以此方法没有用过,记录在这里,有机会验证 ---------------------------------------------------------- ...

  2. iOS: 学习笔记, swift扩展

    // // YYExtension.swift // // Created by yao_yu on 14-7-18. // Copyright (c) 2014年 yao_yu. All right ...

  3. theano中对图像进行convolution 运算

    (1) 定义计算过程中需要的symbolic expression """ 定义相关的symbolic experssion """ # c ...

  4. 通过CTAPI和Citect SCADA软件进行数据通讯

    官方文档 Citect SCADA 7.20 Technical Reference 参考文献 基于Citect远程控制的变流量堆料控制系统 [王玉增,顾英妮,王维 济南大学,机械工程学院 ,Cite ...

  5. bootstrap datepicker时间插件显示位置不对

    bppystrap-datetimepicker.min.js中,修改如下:将原来的        if(!b(this.element)){l=l+document.body.scrollTop}改 ...

  6. 全球顶级专家为你解读:什么是真正的 DevOps?

    [编者按]本文是 Skytap 内容主编 Noel Wurst 对 DevOps Enterprise Summit (DOES)的不完全综述,内容包括了 Noel 和一些与会嘉宾的思考,旨在勾画 D ...

  7. DragSortListView学习总结

    Drag-sort-listview 是一个支持拖拽排序和左右滑动删除功能的自定义ListView,重写了 TouchInterceptor类来提供更加优美的拖拽动画效果. DSLV主要特性: 完美的 ...

  8. GPL,LGPL和BSD等协议注意事项

    使用开源软件必须注意GPL,LGPL和BSD等协议 简而言之,GPL协议就是一个开放源代码协议,软件的初始开发者使用了GPL协议并公开软件的源程序后,后续使用该软件源程序开发软件者亦应当根据GPL协议 ...

  9. [LeetCode#265] Paint House II

    Problem: There are a row of n houses, each house can be painted with one of the k colors. The cost o ...

  10. NOIP2014酱油记

    尘埃落定,来补一下酱油记吧... day-1 晚上老师说有xyz的noip模拟赛,于是果断请假来做(shou)题(nve),题目真是理(S)性(X)愉(B)悦(K),然后就爆零了!感觉noip要爆零滚 ...