beautiful number

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Problem Description
Let A=∑ni=1ai∗10n−i(1≤ai≤9)(n is the number of A's digits). We call A as “beautiful number” if and only if a[i]≥a[i+1] when 1≤i<n and a[i] mod a[j]=0 when 1≤i≤n,i<j≤n(Such as 931 is a "beautiful number" while 87 isn't).
Could you tell me the number of “beautiful number” in the interval [L,R](including L and R)?
Input
The fist line contains a single integer T(about 100), indicating the number of cases.
Each test case begins with two integers L,R(1≤L≤R≤109).
Output
For each case, output an integer means the number of “beautiful number”.
Sample Input
2 1 11 999999993 999999999
Sample Output
10 2
解题思路:根据"beautiful number"的特征,在10^9范围内数的个数肯定较少,故可以先打表求出10^9内的所有"beautiful number",这里可以采用bfs打表生成,然后对于每一次的L和R,采用二分找到相应的范围。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;int table[1000000];
int tot,L,R;bool judge(int u,int k)
{while(u){int tmp = u % 10;if(tmp % k != 0) return false;u = u / 10;}return true;
}void bfs()
{queue<int> q;tot = 0;for(int i = 1; i <= 9; i++) q.push(i);while(!q.empty()){int u = q.front();q.pop();if(u > 1000000000) break;table[++tot] = u;for(int i = 1; i <= 9; i++){if(u % 10 < i) break;if(judge(u,i))q.push(u * 10 + i);}}
}int main()
{int t;bfs();  //先用bfs打表scanf("%d",&t);while(t--){scanf("%d%d",&L,&R);int l = lower_bound(table+1,table+1+tot,L) - table;int r = upper_bound(table+1,table+1+tot,R) - table;printf("%d\n",r - l);}return 0;
}

hdu 5179(bfs打表+二分)相关推荐

  1. hdu1043 经典的八数码问题 逆向bfs打表 + 逆序数

    题意: 题意就是八数码,给了一个3 * 3 的矩阵,上面有八个数字,有一个位置是空的,每次空的位置可以和他相邻的数字换位置,给你一些起始状态 ,给了一个最终状态,让你输出怎么变换才能达到目的. 思路: ...

  2. hdu 1150 Machine Schedule (经典二分匹配)

    //A组n人 B组m人 //最多有多少人匹配 每人仅仅有匹配一次 # include<stdio.h> # include<string.h> # include<alg ...

  3. HDU - 1043 Eight(bfs打表)

    题目链接:点击查看 题目大意:八数码经典问题,给出一个3*3的矩阵,其中随机分布着1~8的数字以及一个空位(我们用x来表示空位),在整个矩阵中,每一次操作都可以将x和他附近的方块互换,问经过多少次操作 ...

  4. 后缀数组 ---- 2018~2019icpc焦作H题[后缀数组+st表+二分+单调栈]

    题目链接 题目大意: 给出nnn个数,定义f[l,r]f[l,r]f[l,r]表示 区间[l,r][l,r][l,r]的最大值,求所有 子区间的最大值的和,要求相同的子区间只能算一次 比如数列 5 6 ...

  5. 完全平方数(打表+二分)

    链接:https://ac.nowcoder.com/acm/contest/5203/C 来源:牛客网 完全平方数 题目描述 多次查询[l,r][l,r][l,r]范围内的完全平方数个数 定义整数x ...

  6. 洛谷P4501/loj#2529 [ZJOI2018]胖(ST表+二分)

    题面 传送门(loj) 传送门(洛谷) 题解 我们对于每一个与宫殿相连的点,分别计算它会作为多少个点的最短路的起点 若该点为\(u\),对于某个点\(p\)来说,如果\(d=|p-u|\),且在\([ ...

  7. hdu 5433(bfs+dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5433 解题思路: dp[i][j][k]表示在(x,y)点,毅力为k时的最小体力.由于每个点可能会走多 ...

  8. hdu 3681(bfs+dfs+状态压缩)

    解题思路:这道题属于图上来回走的问题,可以把重复走的过程弱化,即只强调从u->v的结果,中间经过的节点都不考虑.这道题里面'G','F','Y'是重要的节点,其余的点我们是可以忽略的,也就是说, ...

  9. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

最新文章

  1. pythonsparkpickle_Learning Spark (Python版) 学习笔记(一)----RDD 基本概念与命令
  2. JVM堆新生代分配比例
  3. 本地方法(JNI)——访问数组元素+错误处理
  4. 工作242:关于第二个git仓库提交代码
  5. Python预测2020高考分数和录取情况
  6. String str 与 String str=new String() 区别
  7. 网页内容若使用gzip压缩--获得页面源码
  8. 僵尸启示录开始 Scheming Through Ep1 Mac游戏介绍
  9. 正定矩阵与半正定矩阵
  10. 波特率和比特率的定义及计算
  11. ElasticSearch:text和keyword的区别、fileds作用、mappings结构对查询的影响
  12. 实验室设备管理系统(C语言实现)
  13. VMware 配置局域网内访问
  14. 站群服务器金手指排名稳定,黑帽seo手法使用金手指:黑帽SEO,常见的SEO作弊手...
  15. 【漏洞通告】CVE-2022-36803 Atlassian Jira Align权限提升漏洞
  16. 解决方案:macOS Mojave下Pycharm运行pygame无法加载外星人游戏图片以及无法修改颜色
  17. 北大学子求职经历与建议(IT类)
  18. Django auth用户认证模块
  19. Java 字符串与集合练习——词频统计
  20. 软件企业测试人员的角色与职责

热门文章

  1. 线性拟合polyfit_6.数据分析(1) 描述性统计量和线性回归(2)
  2. 神策数据受邀出席,携手亚马逊云科技共建智能家居新航海时代
  3. 倒计时 3 天 | 神策 2019 数据驱动大会即将开幕
  4. 当遭遇“用户增长”停滞,你应该怎么办?
  5. TPYBoard:一款可以发挥无限创意的MicroPython开发板
  6. Redux其实很简单(原理篇)
  7. 什么是MyCat?为什么要用到MyCat呢?
  8. 「POJ 1135」Domino Effect(dfs)
  9. 使用Python解析nginx日志文件
  10. IE6 png背景图片显示不正常处理