题目链接:http://codeforces.com/problemset/problem/55/D

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 ≤ 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
Input
1
1 9
Output
9
Input
1
12 15
Output
2

题目大意:输入n,m,问你区间[n,m]内有多少个数能被它的不为0的位数整除
首先讲一下这道题用到的东西:
看一下下面的证明
sum%(x*n)%x == sum%x;
证明:设sum = k*x+b
    等号左边:
        sum%(x*n)%x -> (k*x+b)%(x*n)%x
        将k转为ka*n + kb代入;
        (ka*n*x+kb*x+b)%(x*n)%x -> (kb*x+b)%x -> b%x -> b
    等号右边:
        b
左右相等,证明成立
接着看:
那么我们就可以用上式中的x*n对num进行取余,记录其取余后的值,显然,1~9的最小公倍数2520是最合理的x*n。
而在逐位统计时,可以直接由前面位取余后的值来得到包含新一位的新数字取余后的值。
例如 RX(R是已知前面位取余后的值),那么Rx%2520 == (R*10+x)%2520。就不在此废话证了。
我们使用记忆化搜索。
**dfs(len, num, lcm, flag)
len表示迭代的长度,
num为截止当前位的数对2520取余后的值。
lcm为截止当前位的所有数的最小公倍数。
flag表示当前数是否可以任意取值(对取值上限进行判断)**
则可用dp[len][num][lcm]来对其进行记录。
但lcm按2520取值根本开不下,所以对lcm进行离散化,因为lcm一定可以整除2520,所以将1~2520可以整除2520的数进行标记即可,测试后发现只有48个,满足当前情况。
看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<stdio.h>
#include<string.h>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<set>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
const ll mod=1e9+;
const int maxn=1e8+;
const int maxk=+;
const int maxx=1e4+;
const ll maxa=;
#define INF 0x3f3f3f3f3f3f
ll a[],Hash[];
ll dp[][][];
ll gcd(ll n,ll m)
{
return m?gcd(m,n%m):n;
}
ll dfs(ll pos,bool limit,ll sum,ll lcm)//sum是当前位数对2520取余后的值,lam是当前位的最小公倍数
{
if(pos==-)
{
return sum%lcm==;
}
if(!limit&&dp[pos][Hash[lcm]][sum]!=-) return dp[pos][Hash[lcm]][sum];
int up=limit?a[pos]:;
ll ans=;
for(int i=;i<=up;i++)
{
ans+=dfs(pos-,limit&&i==up,(sum*+i)%maxa,i?lcm*i/gcd(lcm,i):lcm);
}
if(!limit) dp[pos][Hash[lcm]][sum]=ans;
return ans;
}
ll solve(ll n)
{
ll p=;
while(n)
{
a[p++]=n%;
n/=;
}
return dfs(p-,,,);
}
int main()
{
ios::sync_with_stdio(false);
memset(Hash,,sizeof(Hash));
int cnt=;
for(int i=;i<=maxa;i++)
{
if(maxa%i==)
Hash[i]=cnt++;
}
int t;
memset(dp,-,sizeof(dp));
cin>>t;
while(t--)
{
ll n,m;
cin>>n>>m;
cout<<solve(m)-solve(n-)<<endl;
}
return ;
}

D. Beautiful numbers的更多相关文章

  1. CodeForces 55D Beautiful numbers

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

  2. [codeforces 55]D. Beautiful numbers

    [codeforces 55]D. Beautiful numbers 试题描述 Volodya is an odd boy and his taste is strange as well. It ...

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

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

  4. Codeforces Round #181 (Div. 2) C. Beautiful Numbers 排列组合 暴力

    C. Beautiful Numbers 题目连接: http://www.codeforces.com/contest/300/problem/C Description Vitaly is a v ...

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

  6. CF 55D - Beautiful numbers(数位DP)

    题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...

  7. Codeforces Beta Round #51 D. Beautiful numbers

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

  8. Beautiful Numbers(牛客网)

    链接:https://ac.nowcoder.com/acm/problem/17385来源:牛客网 题目描述 NIBGNAUK is an odd boy and his taste is stra ...

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

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

  10. 【数位dp】Beautiful Numbers @2018acm上海大都会赛J

    目录 Beautiful Numbers PROBLEM 题目描述 输入描述: 输出描述: 输入 输出 MEANING SOLUTION CODE Beautiful Numbers PROBLEM ...

随机推荐

  1. 洛谷 P4525 & P4526 [模板] 自适应辛普森积分

    题目:https://www.luogu.org/problemnew/show/P4525 https://www.luogu.org/problemnew/show/P4526 学习辛普森积分:h ...

  2. python2.7系统性能监控psutil模块

    系统环境:Centos7.4,系统自带python2.7.5 登录psutil官网,下载psutil的tar包:psutil-5.4.6.tar.gz,并使用命名sha256sum和官网的包进行核对, ...

  3. Queue——C#浅谈

    1.Queue定义 System.Collections.Queue类表示对象的先进先出集合,存储在 Queue(队列) 中的对象在一端插入,从另一端移除. 2.优点 1.能对集合进行顺序处理(先进先 ...

  4. 学习Web前端的好网站推荐

    说明:将其他技术技术网址也搜藏到下面,与君共同进步 1.Jquery专题 http://kb.cnblogs.com/zt/jquery/ 2.Entity Framwork专题 http://kb. ...

  5. Ruby环境搭建与“Hello World”

    Ruby的环境搭建比较简单,在http://rubyinstaller.org/downloads/可以得到Ruby的安装包, 安装过程没什么问题.安装完成之后需要配置一下环境变量: 在PATH中填入 ...

  6. mysql主从服务器复制原理

    在实际企业应用环境当中,单台mysql数据库是不足以满足日后业务需求的.譬如服务器发生故障,没有备份服务器来提供服务的话,业务就得停止.介于这种情况,我们来学习一下mysql主从复制. 将Mysql的 ...

  7. ASPX 关闭子窗口后自动更新父窗口

    Response.Write("<script language:javascript>javascript:window.close();</script>&quo ...

  8. Flask03 路由控制(转换器)、反转、请求方法控制

    1 提出问题 如何实现前端传过去的路径时动态的(即:多个url对应一个url视图函数) 例如: 浏览器中输入 http://127.0.0.1:5000/test/good/ 或者 http://12 ...

  9. 阿里云OSS安装使用问题

    最近一政府客户需要将系统部署到政务网(阿里云,不能连接外网),需要挂载OSSFS,通过官网文档,基本可以按流程完成安装,但是安装过程中遇到的几个问题需要了解一下. 服务器级OSS信息 系统:CentO ...

  10. 存储引擎InnoDB

    InnoDB是MySQL的默认存储引擎, InnoDB支持的最大存储限制是64TB,支持事务安全,支持行锁,支持B树索引,不支持哈希索引和全文索引,支持集群索引,支持数据缓存,支持索引缓存,不支持数据 ...