Codeforces Beta Round #51 D. Beautiful numbers 数位dp
D. Beautiful numbers
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/55/problem/D
Description
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 Input
1
1 9
Sample Output
9
HINT
题意
问你l-r间有多少个数,可以整除他的每个位置上的非0数
题解:
dp[i][j][k]表示,现在考虑到了第i位,在该位我用j之后,的lcm,以及各个数位的lcm为k的种类数(好绕。。。
1-9的lcm是2520,于是我们dp[20][2520][2520]就好了,但是显然我们第三维可以离散化一发,这样空间就开的下了~
然后跑数位dp就好了~
代码:
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std; char num[];
long long dp[][][];
int Lcm[];
long long gcd(long long a,long long b)
{
if(b==)
return a;
return gcd(b,a%b);
}
long long dfs(int p,int mod,int lcm,int limit)
{
if(p<)return (mod%lcm == );
if(!limit && ~dp[p][mod][Lcm[lcm]])
return dp[p][mod][Lcm[lcm]];
int e = limit?num[p]:;
long long res = ;
for(int i=;i<=e;i++)
res+=dfs(p-,(mod*+i)%,i!=?i*lcm/gcd(i,lcm):lcm,limit&&i==e);
if(!limit)dp[p][mod][Lcm[lcm]]=res;
return res;
}
long long solve(long long x)
{
int Num = ;
while(x)
{
num[Num++]=x%;
x/=;
}
return dfs(Num-,,,);
}
int main()
{
int t;scanf("%d",&t);
for(int i=,j=;i<=;i++)
if(%i==)
Lcm[i]=j++;
memset(dp,-,sizeof(dp));
for(int i=;i<t;i++)
{
long long l ,r;
scanf("%lld%lld",&l,&r);
printf("%lld\n",solve(r)-solve(l-));
}
}
Codeforces Beta Round #51 D. Beautiful numbers 数位dp的更多相关文章
- Codeforces Beta Round #51 D. Beautiful numbers(数位dp)
题目链接:https://codeforces.com/contest/55/problem/D 题目大意:给你一段区间[l,r],要求这段区间中可以整除自己每一位(除0意外)上的数字的整数个数,例如 ...
- Codeforces Beta Round #51 D. Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)
Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...
- codeforces 55d//Beautiful numbers// Codeforces Beta Round #51
题意:一个数能整除它所有的位上的数字(除了0),统计这样数的个数. 注意离散化,为了速度更快需存入数组查找. 不要每次memset,记录下已有的长度下符合条件的个数. 数位dp肯定是从高位到低位. 记 ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Educational Codeforces Round 8 D. Magic Numbers 数位DP
D. Magic Numbers 题目连接: http://www.codeforces.com/contest/628/problem/D Description Consider the deci ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
随机推荐
- Android下二维码的扫描
Android平台下 二维码的扫描一般采用: Zxing:参考地址 Zxing功能比较强大,支持条形码和二维码的扫描,用的人也比较多,但是Zxing太大,一般开发简单的app,用起来比较麻烦. 所以网 ...
- 【转】cocos2d-x 3.2 Fast TileMap
概述 在游戏中常常会有丰富的背景元素,如果直接使用大的背景图实现,这会造成资源浪费.TileMap就是为了解决这问题而产生的.Cocos2d-x支持使用Tile地图编辑器创建的TMX格式的地图. Co ...
- js一些平时会用到的
如何屏蔽页面js错误 <script language="javascript"> function killErrors() { re ...
- POJ 2421 Constructing Roads
题意:要在n个城市之间建造公路,使城市之间能互相联通,告诉每个城市之间建公路的费用,和已经建好的公路,求最小费用. 解法:最小生成树.先把已经建好的边加进去再跑kruskal或者prim什么的. 代码 ...
- 色谱峰的类型BB,BV,VB等都是什么意思
B(Baseline):峰在基线处开始或结束V(Valley):峰在谷点线处开始或结束P(Peak): 峰开始或结束与基线贯穿点BB就代表标准的峰:从基线开始出峰,最后峰到基线结束(from base ...
- lightoj 1024 (高精度乘单精度)
题意:给你一些数,求它们的最小公倍数,结果可能会很大. 统计出每个素因子出现的最大次数,把他们相乘即可,需要高精度. #include<cmath> #include<cstdio& ...
- 【剑指offer 面试题12】打印1到最大的n位数
思路: 用n位字符数组表示n位数,通过递归的方式逐层(位)遍历,递归终止时打印. #include "stdio.h" #include "string.h" ...
- Asp.net MVC 处理文件的上传下载
如果你仅仅只有Asp.net Web Forms背景转而学习Asp.net MVC的,我想你的第一个经历或许是那些曾经让你的编程变得愉悦无比的服务端控件都驾鹤西去了.FileUpload就是其中一个, ...
- wuzhicms水印的设置
- 一起刷LeetCode1-Two Sum
感觉有必要重新刷刷题了,为以后找工作做做准备,选择LeetCode+topcoder上的Data Science Tutorials, 争取每天晚上10:00开始刷一道,复习一下相关知识点. ---- ...