Beautiful numbers
来源:互联网 发布:vue.js事件对象event 编辑:程序博客网 时间:2024/05/16 15:42
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).
11 9
9
112 15
2
今天,其实昨天开的。看了一晚上。开始搞数位dp。总结来说就是记忆化搜索,关键是选择好dp的状态和维数,需要维护的东西。只是略微知道一些吧现在算是。
这东西还是得多刷题啊。QWQ
这个题用3维。dp[i][j][k]表示处理第i位,前面的各位数字的最小公倍数为j,当前数为k的个数。
这里直接开肯定是不可能的。我们首先把这个当前数给缩一下,1~9的最小公倍数为2520.所以我们可以先对这个数取模,再判断是不是能整除j。
另外第二维可以哈希处理,可以降低空间。
具体的可以搜一下大神们的题解。这个题是比较经典的了。
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int max_lcm = 2520;int lcm(int a,int b){ return a/__gcd(a,b)*b;}long long dp[20][50][2525];int ha[2525];int num[20];long long dfs(int len,int pre_lcm,int pre_num,int up){ if(!len)return pre_num%pre_lcm == 0; if(!up && dp[len][ha[pre_lcm]][pre_num] != -1)return dp[len][ha[pre_lcm]][pre_num]; int n = up?num[len] : 9; long long res = 0; for(int i = 0 ; i <= n ; ++i) { int now_num = (pre_num*10+i)%max_lcm; int now_lcm = pre_lcm; if(i)now_lcm = lcm(now_lcm,i); res += dfs(len-1,now_lcm,now_num,up && i == n); } if(!up)dp[len][ha[pre_lcm]][pre_num] = res; return res;}long long cal(long long x){ int len = 0; while(x) { num[++len] = x%10; x/=10; } return dfs(len,1,0,1);}int main(){ int t; int cnt = 0; for(int i = 1 ; i <= 2520; ++i)if(max_lcm % i ==0)ha[i] = cnt++; memset(dp,-1,sizeof dp); scanf("%d",&t); long long l,r; while(t--) { scanf("%I64d %I64d",&l,&r); printf("%I64d\n",cal(r) - cal(l-1)); } return 0;}
- Beautiful Numbers
- Beautiful numbers
- Beautiful Numbers
- Beautiful numbers
- Beautiful numbers
- Why are numbers beautiful?
- Codeforces_C - Beautiful Numbers
- codeforces Beautiful Numbers
- UVa 11472 - Beautiful Numbers
- codeforces55D Beautiful numbers
- codeforces300C. Beautiful Numbers
- C. Beautiful Numbers
- E - Beautiful Numbers
- 数位dp Beautiful numbers
- 数位dp, Beautiful numbers
- Beautiful numbers CodeForces
- Beautiful numbers CodeForces
- HOJ-1983-Beautiful numbers
- Android开发环境搭建——集成了Eclipse的ADT Bundle + SDK
- PHP冒泡排序法 | 黄乔国PHP
- 25张图让你读懂神经网络架构
- 关于window.showModalDialog()返回值的学习
- [Unity&特效]使用Projector投影仪组件在哪里下载
- Beautiful numbers
- 关于EventHandlerList的用法简介
- 《高效能人士的七个习惯》书籍概要
- 小白通过JDBC在AndroidStudio一步步来访问MYSQL数据库
- git远程仓库的删除和重
- 关于各类复杂的数组声明,指针数组,数组指针等的理解
- 【高性能MySQL】查询优化
- Satisfactory Pairs HackerRank
- 【JVM】自定义类加载器