【数位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 ...
随机推荐
- ERROR: ...hbase.PleaseHoldException: Master is initializing
同学升级HBase后遇到这个问题,hbase shell,status就可以看到 ERROR: -hbase.PleaseHoldException: Master is initializing 解 ...
- Java实现 LeetCode 700 二叉搜索树中的搜索(遍历树)
700. 二叉搜索树中的搜索 给定二叉搜索树(BST)的根节点和一个值. 你需要在BST中找到节点值等于给定值的节点. 返回以该节点为根的子树. 如果节点不存在,则返回 NULL. 例如, 给定二叉搜 ...
- Java实现 LeetCode 132 分割回文串 II(二)
132. 分割回文串 II 给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回符合要求的最少分割次数. 示例: 输入: "aab" 输出: 1 解释: 进行一 ...
- Java实现最小费用最大流问题
1 问题描述 在最大流有多组解时,给每条边在附上一个单位费用的量,问在满足最大流时的最小费用是多少? 2 解决方案 下面代码所使用的测试数据如下图: package com.liuzhen.pract ...
- Linux 权限管理-ACL权限
ACL权限是为了在现有的所有者.所属组.其他人不够使用的情况下使用的,使用它必须保证文件所在的分区支持ACL df -h:查看系统所有分区信息 dumpe2fs -h /dev/vda1,可以查看分区 ...
- 「MoreThanJava」机器指令到汇编再到高级编程语言
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
- 制作sentinel docker镜像
在sentinel官方下载jar包即可运行,但是在部署的时候一个一个的启动jar包很不方便,制作成镜像方便部署和管理. 1)直接运行 # 修改端口号,默认是8080 java -jar sentine ...
- C#通过模板导出Word的两种方法(超简单)
方法一:使用Office的组件 使用该方法必须要安装Office 1.制作Word模板 在需要填充内容的地方增加标识符号,方便之后替换使用,例如 [项目名称],其中[]符号和中间的文字可根据个人情况进 ...
- 【JAVA进阶架构师指南】之五:JVM性能调优
前言 首先给大家说声对不起,最近属实太忙了,白天上班,晚上加班,回家还要收拾家里,基本每天做完所有事儿都是凌晨一两点了,没有精力再搞其他的了. 好了,进入正题,让我们来聊聊JVM篇最后一个章节 ...
- Pycharm下安装Numpy包
Numpy--Numerical Python,是一个基于Python的可以存储和处理大型矩阵的库.几乎是Python 生态系统的数值计算的基石,例如Scipy,Pandas,Scikit-learn ...