CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D
4 seconds
256 megabytes
standard input
standard output
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.
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 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
题解:
题意:求一段区间内满足“能整除所有位上的数(非0)”的数的个数。
1.可知:如果一个数能被一个集合内的所有数整除,那么它必定能被这个集合上的数的最小公倍数整除。所以,在处理的时候,我们只需要记录最小公倍数。经过计算,1到9的最小公倍数为2520.
2.dp[pos][lcm][num]:处理到pos位,从最高位到pos位上的数的最小公倍数为lcm,且从最高位到pos位所形成的数%2520为num。
代码如下:
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int maxn = ; const int MOD = ; //1……9的最小公倍数为2520; int digit[], M[];
LL dp[][][]; //1 …… 9 组合而成的最小公倍数有48个,故离散化后50个足矣。 LL gcd(LL a, LL b)
{
return b==?a:gcd(b, a%b);
} LL dfs(int pos, LL lcm, LL num, bool lim)
{
if(!pos) return (num%lcm==); //除得尽
if(!lim && dp[pos][M[lcm]][num]!=-) return dp[pos][M[lcm]][num]; LL ret = ;
int maxx = lim?digit[pos]:;
for(int i = ; i<=maxx; i++)
ret += dfs(pos-, i?lcm*i/gcd(lcm, i):lcm, (num*+i)%MOD, lim&&(i==maxx)); if(!lim) dp[pos][M[lcm]][num] = ret;
return ret;
} LL solve(LL n)
{
int len = ;
while(n)
{
digit[++len] = n%;
n /= ;
}
return dfs(len, , , true);
} int main()
{
LL T, n, m;
scanf("%lld", &T);
memset(dp,-,sizeof(dp)); int cnt = ;
/* 原来还可以这样求一个集合的不同组合的最小公倍数的个数。
先算出这个集合所有元素的最小公倍数,那么我们可以得出一个结论:
这个最小公倍数必定能被其他元素组合的最小公倍数整除。根据这个结论,
我们就可以求出一个集合中不同元素组合的最小公倍数的个数
*/
for(int i = ; i<=MOD; i++)
if(MOD%i==)
M[i] = ++cnt;
while(T--)
{
scanf("%lld%lld",&m,&n);
LL ans = solve(n) - solve(m-);
printf("%lld\n", ans);
}
return ;
}
CodeForces - 55D Beautiful numbers —— 数位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(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- codeforces 55D. Beautiful numbers 数位dp
题目链接 一个数, 他的所有位上的数都可以被这个数整除, 求出范围内满足条件的数的个数. dp[i][j][k], i表示第i位, j表示前几位的lcm是几, k表示这个数mod2520, 2520是 ...
- FZU2179/Codeforces 55D beautiful number 数位DP
题目大意: 求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...
- CF 55D. Beautiful numbers(数位DP)
题目链接 这题,没想出来,根本没想到用最小公倍数来更新,一直想状态压缩,不过余数什么的根本存不下,看的von学长的blog,比着写了写,就是模版改改,不过状态转移构造不出,怎么着,都做不出来. #in ...
- 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的水题,觉得 ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
随机推荐
- POJ2096 Collecting Bugs(概率DP,求期望)
Collecting Bugs Ivan is fond of collecting. Unlike other people who collect post stamps, coins or ot ...
- StoryBoard中,TableView位置总是在顶部出现空白的解决
重设TableView的 contentInset 属性可解决. _tableView.contentInset = UIEdgeInsetsMake( -30, 0, 0, 0);
- Elasticsearch使用syslog发送Watcher告警事件
https://blog.csdn.net/mvpboss1004/article/details/70158864?locationNum=9&fps=1
- Codeforces 552E Vanya and Brackets(枚举 + 表达式计算)
题目链接 Vanya and Brackets 题目大意是给出一个只由1-9的数.乘号和加号组成的表达式,若要在这个表达式中加上一对括号,求加上括号的表达式的最大值. 我们发现,左括号的位置肯定是最左 ...
- python多线程(四)
原文:http://www.cnblogs.com/huxi/archive/2010/06/26/1765808.html 本文介绍了Python对于线程的支持,包括“学会”多线程编程需要掌握的基础 ...
- 安装Django时解决的问题-mysql及访问(附pycharm激活)
1.做些软链接和virtualenv的基本使用: ln -s /data/linkdood/im/vrv/python36/bin/python3.6 /usr/bin/python3 ln -s / ...
- Hadoop学习(一)生态体系之简介
Hadoop 系列(一)基本概念 一.Hadoop 简介 Hadoop 是一个由 Apache 基金会所开发的分布式系统基础架构,它可以使用户在不了解分布式底层细节的情況下开发分布式程序,充分利用集群 ...
- cef network-settings
Network Settings 目录 1 System network settings 2 Preference service for network settings 3 Command-li ...
- Adding an Exception Breakpoint - Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 25 bey
用如下的方法可以非常方便停留到具体crash的某行代码 Adding an Exception Breakpoint Add an exception breakpoint to your proje ...
- android 圆形按钮
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...