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中进行视频的分割
最近项目有个需求要对录制的视频进行分割,查了很多资料,看到ffmpeg可以对视频进行分割.上网找到别人基于android的开源ffmpeg,终于编译成功ffmpeg.so.但是要使用的话还要查ffmp ...
- 使用solrj操作solr索引库
(solrj)初次使用solr的开发人员总是很郁闷,不知道如何去操作solr索引库,以为只能用<五分钟solr4.5教程(搭建.运行)>中讲到的用xml文件的形式提交数据到索引库,其实没有 ...
- POJ 1312 Numerically Speaking
题意:a = 1, b = 2, ..., z = 26, aa = 27, ... 给字符串或者数字,输出对应的答案. 解法:类似26进制……但又不完全是……拿java大数模拟了一下…… 代码: i ...
- [转] WinForm自定义函数FindControl实现按名称查找控件
原文地址 WinForm自定义函数FindControl实现按名称查找控件 本文所述实例实现WinForm自定义函数FindControl实现按名称查找控件的功能,在C#程序开发中有一定的实用价值. ...
- 开学了!这些Linux认证你要知道。
导读 大家好,今天我们将认识一些非常有价值的全球认可的Linux认证.Linux认证是不同的Linux专业机构在全球范围内进行的认证程序.Linux认证可以让Linux专业人才可以在服务器领域或相关公 ...
- C# 替换文本文件中的某一行
C# 替换文本文件中的某一行 (要求此文件存在) /// <summary> /// LineIndex 表示新的内容所在的行位置 /// </summary> /// < ...
- css水平居中和垂直居中
水平居中:内联元素:text-align:center;相对于父级居中显示块级元素:margin:0 auto;但是需要同时width,否则无法看到效果多个块级元素居中:在此想要探讨一下display ...
- mongodb 新建用户 -摘自网络
随着版本的更新,对在使用mongodb的业务也进行了版本升级,但是在drop掉一个数据库时,问题来了,原来的用户随着删除库也被删除掉,但是再想通过原来的语法db.addUser()添加,一直报错,提示 ...
- POJ 2349 Arctic Network (最小生成树)
Arctic Network 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/F Description The Departme ...
- Rop 文件上传解决思路
由于服务请求报文是一个文本,无法直接传送二进制的文件内容,因此必须采用某种转换机制将二进制的文件内容转换为字符串.Rop 采用如下的方式对上传文件进行编码:<fileType>@<B ...