题目链接: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
11 9

Output
9

Input
112 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+7;
const int maxn=1e8+10;
const int maxk=100+10;
const int maxx=1e4+10;
const ll maxa=2520;
#define INF 0x3f3f3f3f3f3f
ll a[25],Hash[2550];
ll dp[25][50][2550];
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==-1){return sum%lcm==0;}if(!limit&&dp[pos][Hash[lcm]][sum]!=-1) return dp[pos][Hash[lcm]][sum];int up=limit?a[pos]:9;ll ans=0;for(int i=0;i<=up;i++){ans+=dfs(pos-1,limit&&i==up,(sum*10+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=0;while(n){a[p++]=n%10;n/=10;}return dfs(p-1,1,0,1);
}
int main()
{ios::sync_with_stdio(false);memset(Hash,0,sizeof(Hash));int cnt=0;for(int i=1;i<=maxa;i++){if(maxa%i==0)Hash[i]=cnt++;}int t;memset(dp,-1,sizeof(dp));cin>>t;while(t--){ll n,m;cin>>n>>m;cout<<solve(m)-solve(n-1)<<endl;}return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/9365806.html

D. Beautiful numbers相关推荐

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

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

  2. CodeForces - 55D Beautiful numbers

    题目链接:http://codeforces.com/problemset/problem/55/D 题意:求区间[L,R]有多少个Beautiful numbers.Beautiful number ...

  3. Codeforces Beta Round #51 D. Beautiful numbers 数位dp + 状态优化

    传送门 文章目录 题意: 思路: 题意: 思路: 数位dpdpdp挺经典的一个题辣,有一个很明显的状态就是f[pos][num][lcm]f[pos][num][lcm]f[pos][num][lcm ...

  4. 数位DP CF 55D Beautiful numbers

    题目链接 题意:定义"beautiful number"为一个数n能整除所有数位上非0的数字 分析:即n是数位所有数字的最小公倍数的倍数.LCM(1到9)=2520.n满足是252 ...

  5. Codeforces 55D Beautiful numbers (数位DP)

    题意:有T组询问,每次询问区间[l, r]中的beautiful number有多少.beautiful number是指这个数可以被组成它的数字整除.例如15是beautiful number,因为 ...

  6. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers

    传送门 题意:问你从[1,N]有多少个数能被自身的SOD(sum of digits)整除 题解:数位dp,枚举SOD,因为最多只有12位,所以只要枚举1到12*9,一维记录pos,二维记录当前剩余要 ...

  7. The 2018 ACM-ICPC上海大都会赛 J Beautiful Numbers (数位DP)

    题意:求小于等于N且能被自己所有位上数之和整除的数的个数. 分析:裸的数位dp.用一个三位数组dp[i][j][k]记录:第i位,之前数位之和为j,对某个mod余数为k的状态下满足条件的个数.这里mo ...

  8. CF 55D Beautiful numbers 数位DP

    思路: 要找一个数能被他的所有反的数字整除,只需求出这个数能被其数字的LCM整除.而LCM最大为5*7*8*9=2520: 如果直接开dp[20][2520][2520]会超内存,而2^3,3^2,5 ...

  9. Beautiful Numbers

    https://code.google.com/codejam/contest/5264487/dashboard#s=p1 https://www.cnblogs.com/xiao-lei/p/10 ...

最新文章

  1. C指针5:字符串指针(指向字符串的指针)
  2. 任务流程管理,从繁杂的项目管理中解脱出来
  3. Verilog初级教程(11)Verilog中的initial块
  4. 重新配对_郑思维和陈清晨当年配对也很强,为什么被拆开重新和黄雅琼配对
  5. css中光标的设置,CSS Cursors(光标)
  6. JFreeChart 使用介绍
  7. 从Java执行可执行的命令行
  8. TCP粘包拆包基本解决方案
  9. python django步骤_python - django (ORM使用步骤)
  10. CSS-样式表插入的三种方法、背景(background)、文本
  11. GitHub网页版开始教程
  12. Python人脸识别项目-基础代码
  13. ezcad旋转轴标刻参数_EzCad 2.0 扩展轴标刻插件使用说明书简体中文(.pdf
  14. win7精简_还不升级!Bug最少的win10 LTSC版,极致精简,比win7更快更干净
  15. linux给普通用户添加管理员权限,linux 赋予普通用户管理员权限
  16. 485串口和计算机通信,485串口通信 485通讯与串口通讯区别
  17. android Rect
  18. GF-3双极化SAR遥感影像预处理【基础版】
  19. godot引擎学习10
  20. Genymotion 自配 AndoidSDK 一直Booting

热门文章

  1. ADO.NET_07_OracleDataAdapter
  2. 【已解决】iOS11使用MJRefresh上拉加载结束tableView闪动、跳动的问题
  3. 记一次服务器本地Tomcat能访问,但远程访问不了的解决方案
  4. 深度学习基础 | RNN家族全面解析
  5. linux http用户,HTTP完整请求过程
  6. 从0成为Facebook广告高手系列教程,Facebook广告数据分析上篇
  7. CCF2015-12-2 消除类游戏
  8. Stanford CS230深度学习(三)调参、正则化和优化算法
  9. 吴恩达机器学习学习笔记第九章:神经网络学习
  10. HIbernate抽象出通用方法