【数位dp】CF 55D Beautiful numbers
题目
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
分析
所有的数位是1到9,最小公倍数是5 7 8* 9,是2520,开数组为数位,和,最小公倍数,分别对应dp【20】【2520】【2520】,但是超内存,所以利用hash,把最后的最小公倍数压缩在50以内,因为可能的最小公倍数是48个。
代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
long long a[];
int hash[];
long long dp[][][];
int gcd(int a,int b){
if(a<b)swap(a,b);
while(b){
int w=a;
a=b;
b=w%b;
}
return a;
} long long dpp(int pos,int sum,int b,int limit){
if(pos==-)return sum%b==;
if(!limit&&dp[pos][sum][hash[b]]!=-)return dp[pos][sum][hash[b]];
long long sun=;
int end=limit?a[pos]:;
for(int i=;i<=end;i++){
int q=b;
if(i>=)q=q*i/gcd(q,i);
sun+=dpp(pos-,(sum*+i)%,q,limit&&i==a[pos]); }
if(!limit)dp[pos][sum][hash[b]]=sun;
return sun;
} long long go(long long x){
int pos=;
while(x){
a[pos++]=x%;
x/=;
} return dpp(pos-,,,);
} int main(){
long long n,m,t;
int j;
memset(dp,-,sizeof(dp));
for(int i=,j=;i<=;i++){
if(%i==)hash[i]=j++;
}
scanf("%I64d",&t);
while(t--){
scanf("%I64d%I64d",&n,&m);
printf("%I64d\n",go(m)-go(n-));
} return ;
}
最新文章
- centos虚拟机复制移动后网络配置无效
- java 创建线程的三种方法Callable,Runnable,Thread比较及用法
- Android SDK ADT下载地址
- [转]理解HTTP幂等性
- Struts2(十)OGNL标签二与Struts2标签
- 【RSYSLOG】rsyslog作为日志采集器安装配置说明
- SVN版本管理trunk及branch相关merge操作
- 虚拟键盘,移动web开发的痛
- Codeforces Round #198 (Div. 2) B. Maximal Area Quadrilateral
- Android HttpClient HttpURLConnection相关介绍
- Windows 8.1下 MySQL绿色版安装配置与使用
- [Leetcode] Binary search--436. Find Right Interval
- PHP基础入门(三)---PHP函数基础
- Java基础(7)-异常处理
- 移动HTNL5前端框架—MUI
- java利用反射交换两个对象中的字段相同的字段值
- Linux查看和修改时间、日期
- 洗礼灵魂,修炼python(31)--面向对象编程(1)—面向对象,对象,类的了解
- LightOJ - 1265 (概率)
- 这份书单,给那些想学Hadoop大数据、人工智能的人