Codeforces #55D (数位dp+离散化)
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
9
1
12 15
2 让你找[l,r]区间中,能被自己各个非零数位整除的数的个数。一看就是满足区间减法。现在就讨论怎么求就行了。
首先lcm(1..9)=2520, int MOD=2520;保存每一个数是不现实的,所以我们就.保存x%MOD就行了。
preSum表示已经讨论的前面的几个数位的值(前串),preLcm表示前穿的Lcm。
这里注意到1...9的各种lcm可以离散化处理,只有48个,这样可以大大减少数组的空间。
我们再用flag来表示当前位的数字大小是否超过x对应位的大小
例:x=15666;当我们讨论到千位是1,2,3,4时,后面三位是随便选的,讨论千位是5是,百位就不能随便选了,要<=6,此时在千位我们就达到了边界。
剩下的交给dfs。
PS:有人把2520优化成252的,92ms过了...我1122ms...
代码如下:
#include <bits/stdc++.h> using namespace std; const int MAXN=;
const int MOD=;
long long dp[MAXN][MOD][];//dp[i][j][k]表示处理到第i位,前串数(取模后)是j,前串树lcm是k时,后面位随便变的合法情况的个数
int index[MOD+],bit[MAXN];//index表示1..9的各种组合lcm,bit是将数字的每一位拆开保存
long long int gcd (long long int a,long long int b) {return (b==)?a:gcd(b,a%b);}
long long int lcm (long long int a,long long int b){return a/gcd(a,b)*b;}
void init()//来找1...9之间各种组合的lcm
{
int num=;
for (int i=;i<=MOD;++i)
if (MOD%i==)
index[i]=num++;
}
long long dfs (int pos,int preSum,int preLcm,bool flag)//pos当前位,flag前面几位是否达到边界
{
if (pos==-)//讨论到最后一位
return preSum%preLcm==;//如果这个数满足要求,+1
if (!flag && dp[pos][preSum][index[preLcm]]!=-)//没达到边界而且访问过这个状态
return dp[pos][preSum][index[preLcm]];//直接return,记忆化搜索
long long ans=;
int endd=flag?bit[pos]:;//这位达到边界时,下一位从0到x的对应位变化。没达到边界是0...9变化
for (int i=;i<=endd;i++)
{
int nowSum=(preSum*+i)%MOD;//添加下一位数字,然后更新状态
int nowLcm=preLcm;
if (i)
nowLcm=lcm(nowLcm,i);
ans+=dfs(pos-,nowSum,nowLcm,flag&&i==endd);
}
if (!flag)
dp[pos][preSum][index[preLcm]]=ans;
return ans;
}
long long calc (long long x)
{
memset(bit,,sizeof bit);
int pos=;
while (x)
{
bit[pos++]=x%;
x/=;
}
return dfs(pos-,,,);
}
int main()
{
int t;
long long int l,r;
init();
memset(dp,-,sizeof dp);
scanf("%d",&t);
while (t--)
{
scanf("%I64d%I64d",&l,&r);
printf("%I64d\n",calc(r)-calc(l-));
}
return ;
}
Codeforces #55D (数位dp+离散化)的更多相关文章
- Codeforces 55D (数位DP+离散化+数论)
题目链接: http://poj.org/problem?id=2117 题目大意:统计一个范围内数的个数,要求该数能被各位上的数整除.范围2^64. 解题思路: 一开始SB地开了10维数组记录情况. ...
- codeforces 55D 数位dp
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- codeforces 55D - Beautiful numbers(数位DP+离散化)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 628D 数位dp
题意:d magic number(0<=d<9)的意思就是一个数,从最高位开始奇数位不是d,偶数位是d 题目问,给a,b,m,d(a<=b,m<2000)问,a,b之间有多少 ...
- codeforces 401D (数位DP)
思路:很明显的数位dp,设dp[i][j] 表示选取数字的状态为i,模m等于j的数的个数,那么最后的答案就是dp[(1<<n)-1][0].状态转移方程就是,dp[i|(1<< ...
- Travelling Salesman and Special Numbers CodeForces - 914C (数位dp)
大意: 对于一个数$x$, 每次操作可将$x$变为$x$二进制中1的个数 定义经过k次操作变为1的数为好数, 求$[1,n]$中有多少个好数 注意到n二进制位最大1000位, 经过一次操作后一定变为1 ...
- Codeforces - 914C 数位DP
题意有点难以描述,简略的就是给定一个二进制\(n\),每一步操作能使\(n\)的位为1的数的和转化为一个十进制,然后转化为该数的二进制再进行相同的操作 查询\([0,n]\)中操作数恰好为\(k\)的 ...
- Codeforces 13C Sequence --DP+离散化
题意:给出一个 n (1 <= n <= 5000)个数的序列 .每个操作可以把 n 个数中的某一个加1 或 减 1.问使这个序列变成非递减的操作数最少是多少 解法:定义dp[i][j]为 ...
随机推荐
- MySQL/RDS数据如何同步到MaxCompute之实践讲解
摘要:大数据计算服务(MaxCompute,原名ODPS)是阿里云提供的一种快速.完全托管的EB级数据仓库解决方案.本文章中阿里云MaxCompute公有云技术支持人员刘力夺通过一个实验向大家介绍了阿 ...
- _stdcall
__cdecl __fastcall与__stdcall,三者都是调用约定(Calling convention),它决定以下内容:1)函数参数的压栈顺序,2)由调用者还是被调用者把参数弹出栈,3)以 ...
- The shortest problem(hdu,多校
The shortest problem Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Oth ...
- 课下选作Main dc
一.中后缀定义: 中缀表达式:我们平时写的数学表达式一般为中缀表达式,如"5+2(3(3-12+1))",直接拿中缀表达式直接让计算机计算表达式的结果并不能做到. 后缀表达式:把中 ...
- Http协议面试题(总结)
Http协议面试题(总结) 一.总结 一句话总结: 主要考常见的状态码,以及https,其它的多抓抓包就熟悉了 1.说一下什么是Http协议? 数据传输的格式规范:对器客户端和 服务器端之间数据传输的 ...
- Linux sudo 详解
简单的说,sudo 是一种权限管理机制,管理员可以授权于一些普通用户去执行一些 root 执行的操作,而不需要知道 root 的密码.严谨些说,sudo 允许一个已授权用户以超级用户或者其它用户的角色 ...
- nRF51822 蓝牙低功耗和 2.4GHz 专利 SoC
DESCRIPTION nRF51822 是功能强大.高灵活性的多协议 SoC,非常适用于 Bluetooth® 低功耗和 2.4GHz 超低功耗无线应用. nRF51822 基于配备 256kB f ...
- HTML最全标签
一.HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(DTD). 标签:a 说明:标明超链接的起始或目的位置. 标签:acronym 说明:标明缩写词. ...
- Pipenv管理项目环境--Django项目的一些最佳实践
virtualenv --- 使用不方便 提升效率,管理更便捷--- pipenv 新建环境:: pip3 install pipenv 在项目下,用pipenv安装 Djagno pipenv in ...
- Modify PDF operators.
1 Depart Process: 2 1. Grep xref and trailer binary position in file. 3 2. Dump xref table and trail ...