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

Sample test(s)
input
1
1 9
output
9
input
1
12 15
output
2

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

题意
找区间[l,r]中的B数:该数能被其每一位数字整除。 分析
能被每一位数字整除,则能被所有位上的数字的lcm(最小公倍数)整除。而1到9的lcm为2520,于是我们可以进行数位dp,dp[i][j][k]表示到第i位时,
该数为j,且此前各数位的lcm为k。可是很显然,数值j不能直接表示出来,我们得想个办法优化。因为递归到最后实质是求x%lcm,那么我们可以想到一个性质:
x%m==x%(km)%m,这样,x%lcm==x%2520%lcm,于是第二维就缩减到2520以内了。现在空间复杂度则为20*2520*2520,还是爆炸。思考一下哪里可以优化?
突然发现,真正作为最小公倍数的个数并没有那么多,只有48个。至此,空间复杂度就变成了20*2520*50,接下来就是记忆化递归求解了,要注意边界的情况。
详情看代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<queue>
#include<vector>
#include<bitset>
#include<map>
#include<deque>
#include<stack>
using namespace std;
typedef pair<int,int> pii;
#define X first
#define Y second
#define pb push_back
#define mp make_pair
typedef long long ll;
#define ms(a,b) memset(a,b,sizeof(a))
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+;
const int mod = 1e9+;
const int mm = ;
#define lson l,m,2*rt
#define rson m+1,r,2*rt+1
ll dp[][][];
int has[];
int bit[];
ll gcd(ll a,ll b){
return (b==)?a:gcd(b,a%b);
}
ll lcm(ll a,ll b){
return a/gcd(a,b)*b;
}
void init(){
int tot=;
for(int i=;i<;i++){
if(mm%i==){
has[i]=tot++;
}
}
} ll dfs(int pos,int preSum,int preLcm,bool limit){
if(pos<) return preSum%preLcm==; //到最后一位
if(!limit && dp[pos][preSum][has[preLcm]]!=-)
return dp[pos][preSum][has[preLcm]];//记忆化
ll ans=;
int ed = limit?bit[pos]:; //该位有边界
for(int i=;i<=ed;i++){
int nowSum = (preSum*+i)%mm;
int nowLcm = preLcm;
if(i) nowLcm = lcm(nowLcm,i);
ans += dfs(pos-,nowSum,nowLcm,limit && i==ed);
}
if(!limit) dp[pos][preSum][has[preLcm]]=ans;
return ans;
}
ll solve(ll x){
int tot=;
while(x){
bit[tot++]=x%;
x/=;
}
return dfs(tot-,,,);
} int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
#endif // LOCAL
int t;
ll l,r;
ms(dp,-);
init();
scanf("%d",&t);
while(t--){
cin>>l>>r;
cout<<solve(r)-solve(l-)<<endl;
}
return ;
}
 

Codeforces #55D-Beautiful numbers (数位dp)的更多相关文章

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

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

  2. CodeForces - 55D - Beautiful numbers(数位DP,离散化)

    链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...

  3. CodeForces - 55D Beautiful numbers —— 数位DP

    题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...

  4. Codeforces - 55D Beautiful numbers (数位dp+数论)

    题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...

  5. codeforces 55D. Beautiful numbers 数位dp

    题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...

  6. FZU2179/Codeforces 55D beautiful number 数位DP

    题目大意: 求  1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...

  7. CF 55D. Beautiful numbers(数位DP)

    题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...

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

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

  9. Codeforces 55D. Beautiful numbers(数位DP,离散化)

    Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...

  10. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

随机推荐

  1. elasticsearch同步mongodb--mongo connector的使用

    部署准备 python-3.6.4-amd64.exe mongodb-win32-x86_64-3.4.6-signed.msi  (如果已经安装可以忽略) 注意点! 之前我写的一篇文章用的是ela ...

  2. AngularJS + CoffeeScript 前端开发环境配置详解

    AngularJS 号称 '第一框架' ('The first framework') 确实是名不虚传.由其从jQuery中完全转入AngularJS后就有无法离开他的感觉了.虽然AngularJS的 ...

  3. win10+anaconda3+python3.6+opencv3.1.0

    最近用windows系统比较多,就想在win10下搞一下深度学习这一方面的研究,那么就需要配置好环境巴拉巴拉的一堆东西.默默记个笔记,正所谓“好记性不如烂笔头”. 1.安装Anaconda 这个是一个 ...

  4. JQ_五星级评分特效

    代码如下:<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" conte ...

  5. Salesforce Apex学习 : 利用Schema命名空间中的DescribeSObjectResult类型来获取sObject对象的基本信息

    DescribeSObjectResult 对象的取得: //使用getDescribe方法和sObject token Schema.DescribeSObjectResult mySObjDesc ...

  6. selenium+ python自动化--断言assertpy

    前言: 在对登录验证时,不知道为何原因用unittest的断言不成功,就在网上发现这个assertpy,因此做个笔记 准备: pip install assertypy 例子: from assert ...

  7. 按键精灵对APP自动化测试(下)

    上一篇介绍了安卓app上使用按键精灵的实践,这里再来说说苹果上的app. 由于iOS相关工具对操作系统的限制,目前在iOS10.0.2系统上应用成功. 二.       苹果手机按键精灵APP录制 适 ...

  8. Unity角色对话

    对话类------------------------------------------------------------------------------------------------- ...

  9. Beta阶段展示博客

    Beta阶段展示博客 1. 团队成员的简介和个人博客地址 刘畅 博客园ID:森高Slontia 身份:PM 个人介绍: 弹丸粉 || 小说创作爱好者 || 撸猫狂魔(x || 生命的价值在于创造 (我 ...

  10. MVC 如何设定默认默认路由为指定的Area下的某个action(笔记)

    感谢这篇博文的作者:http://blog.csdn.net/chenqiangdage/article/details/48136601 最近使用VS2015开发一个统一认证服务,想将API自己生成 ...