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 ...
随机推荐
- 【转】SDP file
SDP file Introduction The Session Description Protocol (SDP) is a format for describing the initiali ...
- POJ 1094 Sorting It All Out
题意:给出m对关于n个字母的小于关系,输出通过这些关系能得到的结论,如果可以排序就输出至少知道第几个关系时就可以知道顺序,从小到大输出顺序:如果产生歧义就输出在第几个关系时出现歧义,如果不能得出准确的 ...
- 对delegate进行扩展 打造通用的"计时完成"方法 z
让用户尽量少打字 每次让用户输入这么多信息的确很糟糕, 可以改进一下设计: 服务器IP和用户名可以存放在配置文件里面, 初始化的时候默认加载到相应的文本框中; 从安全角度考虑, 密码必须经过用户手动输 ...
- 翻译【ElasticSearch Server】第一章:开始使用ElasticSearch集群(7)
检索文档(Retrieving documents) 我们已经有文档存储在我们的实例.现在,让我们尝试检索它们: curl -XGET http://localhost:9200/blog/artic ...
- HDU 4267-A Simple Problem with Integers(多个BIT)
题意: 2种操作 1 a b k c 在区间[a,b]中的(i-a)%k==0的位置i上的数+c 2 a 查询位置a的值 输出每次查询的值 分析: 开始想到多维的线段树,但比较麻烦,看了题解才知道,用 ...
- WinForm使用webbrowser爬取数据,中文乱码问题
使用winform中的webbrowser抓取某个网站的数据时,遇到中文乱码的问题: 当时使用的获取网页内容的代码为: webBrowser1.DocumentText.ToString(); 不管我 ...
- <转>配置DNS辅助服务器:DNS系列之四
配置DNS辅助服务器 在前面的博文中,我们介绍了如何在DNS服务器中创建常用的DNS记录,本文中我们要为大家介绍如何配置DNS的辅助服务器,同时也要介绍一下和辅助区域类似的存根区域. DNS辅助服 ...
- 建立简单的VLAN通信
http://minitoo.blog.51cto.com/4201040/786011(转载) 在路由器上做单臂路由实现VLAN间路由,也就是设置子接口和封装协议. 实现环境如下图: 在交换机上建立 ...
- bzoj 1109 [POI2007]堆积木Klo(LIS)
[题意] n个数的序列,删除一个数后序列左移,求最后满足i==a[i]的最大个数. [思路] 设最终得到a[i]==i的序列为s,则s应满足: i<j,a[i]<a[j],i-a[i]&l ...
- Tkinter教程之Text(1)篇
本文转载自:http://blog.csdn.net/jcodeer/article/details/1811343 '''Tkinter教程之Text篇(1)''''''1.创建第一个Text''' ...