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 有些时候因为问题的一些"整体性"而导致在按位统计的过程中不能顺便计算出某些量,所以只能在枚举到最后一位 ...
随机推荐
- mysql中不同事务隔离级别下数据的显示效果--转载
事务是一组原子性的SQL查询语句,也可以被看做一个工作单元.如果数据库引擎能够成功地对数据库应用所有的查询语句,它就会执行所有查询,如果任何一条查询语句因为崩溃或其他原因而无法执行,那么所有的语句就都 ...
- WIndows 7 与 Debian 7 双系统启动引导
以前写有一篇文章是Windows XP下引导Linux的(http://www.cnblogs.com/ibgo/archive/2012/05/20/2510736.html),采用的是grub4d ...
- Android_Gallery
xml布局 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...
- git 撤销修改以及删除文件
撤销修改 1.如果当你修改了代码,然后又发现修改错误以后,想撤销前面的操作的时候该怎么办呢? 既然错误发现得很及时,就可以很容易地纠正它.你可以删掉最后一行,手动把文件恢复到上一个版本的状态.如果用 ...
- [转]SQL Server 和Oracle 数据类型对应
本文转自:http://blog.sina.com.cn/s/blog_681cd80d0100q84t.html SqlServer 2k转换为Oracle 10g 列名 SqlServer数据类型 ...
- [转]ODBC连接ORACLE数据库的设置
本文转自:http://www.cppblog.com/edog/articles/1420.html 首先安装Oracle,以Oracle 817为例,作为ODBC开发者的客户端,才能使用Oracl ...
- 关于Eclipse中的开源框架EMF(Eclipse Modeling Framework)
Eclipse项目本身可以划分为4个主要的子项目:Equinox,平台,Java开发工具(Java Development Tools,JDT)和插件开发环境(Plug-in Development ...
- Spring MVC中基于注解的 Controller
终于来到了基于注解的 Spring MVC 了.之前我们所讲到的 handler,需要根据 url 并通过 HandlerMapping 来映射出相应的 handler 并调用相应的方法以响 ...
- android的平台架构及特性
Android平台采用了整合的策略思想,包括底层Linux操作系统.中间层的中间件和上层的Java应用程序.下面我把Android的特性及其架构体系结构总结一下. 一.Android的平台特性 And ...
- 【转】Linux网络相关查询脚本
1. 查看TCP连接状态 netstat -nat |awk '{print $6}'|sort|uniq -c|sort -rn netstat -n | awk '/^tcp/ {++S[$NF] ...