Codeforces 55D Beautiful Number
Codeforces 55D Beautiful Number
a positive integer number is beautiful if and only if it is divisible by each of its nonzero digits.
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).
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).
1
1 9
9
1
12 15
2
思路:
- 在mod中,有一个规律,X%a = X%(b*a)%a; <=> X%( lcm(1,2,...,9) = 2520)%lcm(d[i]) == 0;即可将数值直接降到2520以内;
- 同时一个mod后的数,还需要记录的就是lcm(d[i]),这样每次又计算出来的子结构就直接相加即可(指mod之后的数值以及lcm相同的数(这时就可以看成是一个数)),lcm总共只有48个,(2^3,3^2,5,7的组合 4*3*2*2);
- dp[i][j][k]: [i]: 高位为第i位,[j] : 在mod 2520之后的数值,[k]:记录下高位的lcm,由于直接来会MLE,所以离散化了(使用标号index[]);
代码思路参考了:AC_Von
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int (i)= 0;i < (n);i++)
#define MS0(a) memset(a,0,sizeof(a))
#define MS1(a) memset(a,-1,sizeof(a))
typedef long long ll;
const int MOD = ;
ll dp[][][];
int d[],index[MOD+];
void init()
{
for(int i = ,tot = ;i <= MOD;i++)
if(MOD % i == ) index[i] = tot++;
MS1(dp);
}
int lcm(int a,int b)
{
return a/__gcd(a,b)*b;
}
ll dfs(int pos,int prev,int prelcm,int edge)
{
if(pos == -) return prev % prelcm?:; // ***
ll ans = dp[pos][prev][index[prelcm]];
if( !edge && ~ans) return ans;
ans = ;
int e = edge ? d[pos]:;
for(int i = ;i <= e;i++){
int nowlcm = i ? lcm(prelcm,i) : prelcm;
int nowv = (prev * + i)%MOD;
ans += dfs(pos - ,nowv,nowlcm,edge && i == e);
}
if(!edge) dp[pos][prev][index[prelcm]] = ans;
return ans;
}
ll query(ll n)
{
MS0(d);int tot = ;
while(n){
d[tot++] = n%;
n /= ;
}
return dfs(tot - ,,,);
}
int main()
{
init();
int T;
cin>>T;
while(T--){
ll l,r;
scanf("%I64d%I64d",&l,&r);
printf("%I64d\n",query(r) - query(l-));
}
}
Codeforces 55D Beautiful Number的更多相关文章
- Codeforces 55D Beautiful Number (数位统计)
把数位dp写成记忆化搜索的形式,方法很赞,代码量少了很多. 下面为转载内容: a positive integer number is beautiful if and only if it is ...
- FZU2179/Codeforces 55D beautiful number 数位DP
题目大意: 求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...
- 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 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 55D Beautiful numbers
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- 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 (SPOJ JZPEXT 数位DP)
题意 求[X,Y]区间内能被其各位数(除0)均整除的数的个数. CF 55D 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...
随机推荐
- C#_MVC3之使用Authorize简单的验证登录(一)
第一步,自定义 AuthorizeAttribute. public class CustomAuthorizeAttribute : AuthorizeAttribute { public over ...
- Redis的安装、配置 --转载
原文地址:http://blog.sina.com.cn/s/blog_505bf9af0101ehhp.html redis的安装.配置 安装步骤如下:下载redis安装包:$ cd /opt/ap ...
- TCP连接的建立与关闭
TCP是主机对主机层的传输控制协议:建立连接要三个握手,断开连接要四次挥手. 位码即TCP标志位,有6种标示:SYN(synchronous建立联机),ACK(acknowledgement 确认), ...
- 圆满完成Web安全测试培训课程广州公开班!
圆满完成Web安全测试培训课程广州公开班! http://gdtesting.com/news.php?id=187 下期<Web安全测试最佳实践>公开课通知:8月9.10日地点:广州 课 ...
- c++算法联系,冒泡排序,bubble sort,插入排序,insert sort,
#include <iostream.h> #define MAX 100 void dispaly(int a[],int n) { for(int i=0;i<n;i+ ...
- (转)fastdfs group通过添加硬盘扩容
通过给group的机器添加硬盘的方式,实现某个group的扩容. fastdfs在一台服务器支持多个store_path,每个store_path指向一个存储路径.url “M00/3F/E1/oYY ...
- iframe实现面页无刷新提交表单
一.表单提交到了哪里? 这似乎是个无知的问题,我们都知道表单提交到服务器,java,php,asp等服务器,然后由服务器去读.那么之后呢,服务器总要返回点什么吧,要么返回 一个xml或json数据,要 ...
- Android开发了解——ODEX
ODEX是安卓上的应用程序apk中提取出来的可运行文件,即将APK中的classes.dex文件通过dex优化过程将其优化生成一个·dex文件单独存放,原APK中的classes.dex文件会保留.这 ...
- .NET学习笔记(4) — C#数据类型
目录 一:C#数据类型介绍 二:值类型和引用类型的区别和联系? 三:堆内存和栈内存? 四:参考资料 一:C#数据类型介绍 1:初识C#预定义数据类型 在C#的语言体系中,表示具体数据格式的规范 ...
- 通过样式调整input 中password text默认长度
原文出处 <input >标签内的type分别为password和text时其默认长度和宽度不一致,而在做登陆框时往往需要将它们的长度和宽度设置一致.如下的方法可以通过css控制使其一致: ...