【数位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 ;
}
【数位dp】CF 55D Beautiful numbers的更多相关文章
- 数位DP CF 55D Beautiful numbers
题目链接 题意:定义"beautiful number"为一个数n能整除所有数位上非0的数字 分析:即n是数位所有数字的最小公倍数的倍数.LCM(1到9)=2520.n满足是252 ...
- CF 55D - Beautiful numbers(数位DP)
题意: 如果一个数能被自己各个位的数字整除,那么它就叫 Beautiful numbers.求区间 [a,b] 中 Beautiful numbers 的个数. 分析:先分析出,2~9 的最大的最小公 ...
- CF 55D Beautiful numbers (数位DP)
题意: 如果一个正整数能被其所有位上的数字整除,则称其为Beautiful number,问区间[L,R]共有多少个Beautiful number?(1<=L<=R<=9*1018 ...
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- 【数位DP】CF55D Beautiful numbers
$dp[x][p][pp]$表示第x位,当前已有数字mod 2520(1~9数字的lcm)为p,当前各位数字的lcm为pp 观察到数组太大,考虑压缩,第三维lcm最多只有9个数字,打表发现最多只有48 ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
- [Codeforces-div.1 55D] Beautiful numbers
[Codeforces-div.1 55D] Beautiful numbers 试题分析 还是离散化...\(f_{i,j,k}\)表示i位,gcd为j,余数为k. #include<iost ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- JDBC化繁为简
众所周知,jdbc可谓是java连接数据库最基本的方法,通过DriverManager拿到connection,再从connection拿到statement,再从statement中进一步操作得到结 ...
- Java实现 蓝桥杯 算法训练 Balloons in a Box
试题 算法训练 Balloons in a Box 问题描述 你要写一个程序,使得能够模拟在长方体的盒子里放置球形的气球. 接下来是模拟的方案.假设你已知一个长方体的盒子和一个点集.每一个点代表一个可 ...
- Java实现 LeetCode 500 键盘行
500. 键盘行 给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词.键盘如下图所示. 示例: 输入: ["Hello", "Alaska", & ...
- Java实现 LeetCode 430 扁平化多级双向链表
430. 扁平化多级双向链表 您将获得一个双向链表,除了下一个和前一个指针之外,它还有一个子指针,可能指向单独的双向链表.这些子列表可能有一个或多个自己的子项,依此类推,生成多级数据结构,如下面的示例 ...
- Java实现 洛谷 P2118 比例简化
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.i ...
- Java实现第八届蓝桥杯兴趣小组
兴趣小组 为丰富同学们的业余文化生活,某高校学生会创办了3个兴趣小组 (以下称A组,B组,C组). 每个小组的学生名单分别在[A.txt],[B.txt]和[C.txt]中. 每个文件中存储的是学生的 ...
- 4.keras-交叉熵的介绍和应用
keras-交叉熵的介绍和应用 1.载入数据以及预处理 import numpy as np from keras.datasets import mnist from keras.utils imp ...
- (一)DB、DBMS、SQL之间的关系
一.概念 DB:数据库(database)相当于一个仓库,用于有组织的采存储数据. DBMS:数据库管理系统(database manage system)数据库是通过DBMS来创建和操作,种类很多( ...
- 列表、元组、字典和简单if语句【python实验1】
第一次实验报告: 学生姓名 总成绩 tom 90 jack 89 john 96 kate 86 peter 100 实验内容3-1 建立两个列表分别对学生的姓名和总成绩信息进行存储 name=['t ...
- Linux下自己和自己用各种方法进行文件的上传下载
环境: Ubuntu 16.04 1.SCP # 上传 scp /home/sea/Desktop/test.sh sea@192.168.1.31:/home/sea/Desktop/test.sh ...