18005 It is not ugly number
18005 It is not ugly number
时间限制:2000MS 内存限制:65535K
提交次数:0 通过次数:0
题型: 编程题 语言: G++;GCC
Description
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ...shows the
first 10 ugly numbers. By convention, 1 is included. Then, here are the first 10 Not ugly numbers:7, 11, 13, 14, 17, 19,
21, 22, 23, 26. Given the integer n, write a program to find and print the n'th Not ugly number.
输入格式
First line is T(T<=10000), the number of cases.
The T lines following. Each line of the input contains a positive integer n (n <= 100000000).
输出格式
For each case, output the n'th Not ugly number .
输入样例
3
1
2
9
输出样例
7
11
23
作者
admin
要求第n项非丑数;无疑要先利用数学知识求出丑数。我的做法是先预处理前1200个丑数,至于为什么只用处理这个数量的丑数是自己试出来的;可能你会疑惑为什么只用处理一千多个就行了,,因为丑数是因子只含有2、3和5的数字,所以越往后面数字越大它继越有可能包含有不止这三个数的因子;所以前几百项的丑数序列看似是挺密集的,但越到后面每两个丑数间的间隔就越大。而我们要找的第n项非丑数,肯定就在某两个丑数之间。
所以题目唯一的难点就在于求出丑数。我这里用的是优先队列来预处理。 一开始,我们知道前几项丑数:1、2、3、5;那么 如果要得出接下来同样也是因子只有2、3、5的丑数,就只需要将前面这几个已知道的丑数分别乘以2、3、5得出来的数,那它的因子同样一定是只有2、3、5的。 但是,如果将每一项都乘以这三个数的话,你会发现有重复的数字出现。因而这里要用到一个技巧:给每一个数字加多一个标记,用来标注当前这个丑数是由前面的丑数乘以哪个数字得出来的;所以用到了pair<int,int>。第一个数据域存放这个丑数的值,第二个则用来存放标记值。 比如用2分别乘以2、3、5得到4、6、10,那么得到三个pair为<4,2>、<6,3>、<10,5>;然后将这三个pair存入优先队列中去,弹出2;每次再取出最小的丑数(要从小到大取再加上刚刚说的技巧才能避免重复);假设取到pair<6,3>的时候,这时你就只能将6分别乘以3和5,而不能乘2 ( 因为前面会有<4,2>中的4乘以3得到12发生重复 )。 另外,关于得出了丑数之后再求非丑数的技巧写在了代码注释中了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <utility>
#include <vector>
#define ll long long
#define inf 0x3f3f3f3f
#define MAXN 100000
using namespace std; typedef pair<int,int> node_type;
int res[];
int main()
{
//freopen("input.txt","r",stdin);
priority_queue<node_type,vector<node_type>,greater<node_type> > pq;//用来存放pair的优先队列,默认是比较pair中的第一个元素,greater<>则另其每次去最小的数据
pq.push(node_type(,)); //压入第一个丑数
node_type temp;
for(int i=; i<; i++)
{
temp=pq.top(); //取队列首,也就是当前队列中最小的丑数
switch(temp.second) //由pair<>的第二个元素判断当前这个丑数是由哪个标记值生成的;然后继续生成新的丑数
{
case :
pq.push(make_pair(temp.first*,));
case :
pq.push(make_pair(temp.first*,));
case :
pq.push(make_pair(temp.first*,));
}
res[i]=temp.first;
pq.pop();
}
//处理完丑数后
int t,n,cnt,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
cnt=;
i=;
while(cnt<n) //cnt标记当前已寻找到的第cnt项的非丑数
{
cnt+=(res[i+]-res[i++]-);//cnt每次加上的值是每相邻两个丑数的差,这个差即这两个丑数之间包含的非丑数的个数
}
//找到所要的第n项非丑数在哪两个丑数之间后退出循环
i--;
cnt-=(res[i+]-res[i]-);//从这个丑数区间的左边开始算,res[i]加上n-cnt就是第n项丑数了
printf("%d\n",res[i]+n-cnt);
}
return ;
}
18005 It is not ugly number的更多相关文章
- [LeetCode] Super Ugly Number 超级丑陋数
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- [LeetCode] Ugly Number II 丑陋数之二
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number 丑陋数
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- 【13_263】Ugly Number
简单题 Ugly Number My Submissions Question Total Accepted: 32635 Total Submissions: 94009 Difficulty: E ...
- Leetcode 313. super ugly number
Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all ...
- Leetcode 264. Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- LeetCode 263 Ugly Number
Problem: Write a program to check whether a given number is an ugly number. Ugly numbers are positiv ...
- [LeetCode] Ugly Number II
Write a program to find the n-th ugly number. Ugly numbers are positive numbers whose prime factors ...
- [LeetCode] Ugly Number
Ugly Number Total Accepted: 20760 Total Submissions: 63208 Difficulty: Easy Write a program to check ...
随机推荐
- 洛谷P1040 加分二叉树(区间dp)
P1040 加分二叉树 题目描述 设一个n个节点的二叉树tree的中序遍历为(1,2,3,…,n),其中数字1,2,3,…,n为节点编号.每个节点都有一个分数(均为正整数),记第i个节点的分数为di, ...
- hdu2089不要62(数位dp)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- CSS浮动的处理
之前已经发过一遍有关浮动的解决办法,今天看到一些资料后又有了新的想法: 在CSS布局中float属性经常会被用到,但使用float属性后会使其在普通流中脱离父容器,让人很苦恼 1 浮动带来布局的便利, ...
- PCL: 根据几何规则的曲面剖分-贪婪法表面重建三角网格
点云场景中进行物体识别,使用全局特征的方法严重依赖于点云分割,难以适应杂乱场景.使用局部特征,即对点云进行提取类似于3D SURF.ROPS之类的局部特征,需要寻找离散点云块的局部显著性. 点云的基本 ...
- 【sqli-labs】 less16 POST - Blind- Boolian/Time Based - Double quotes (基于bool型/时间延迟的双引号POST型盲注)
' or 1=1# -->失败 1" or 1=1# -->失败 1') or 1=1# -->失败 1") or 1=1# -->成功 判断为双引号变形注 ...
- spring 回顾
主要就是它的IOC理念 即:把对象的创建.初始化.销毁等工作交给spring容器来做 依赖jar
- matlab学习下拉菜单
用matlab添加listbox控件 修改string和value值,value为几就对应第几行字符串 添加button按钮,将string值改为“选择x轴参数”,字体大小为10 再添加一个按钮,将s ...
- BZOJ : [Usaco2013 Nov]Crowded 单调队列
正反两遍个来一次单调队列 DP 即可. Code: #include<cstdio> #include<deque> #include<algorithm> usi ...
- 【转载】JSTL和EL的使用
使用JSTL前的准备 想要使用JSTL,首先需要给工程导入JSTL的包(JSTL.jar和standard.jar). JSTL标签库 在JSTL中分为以下五个标签 核心标签 格式化标签 SQL标签 ...
- UIAutomator定位简介
UIAutomator元素定位是 Android 系统原生支持的定位方式,虽然与 xpath 类似,但比它更加好用,且支持元素全部属性定位.定位原理是通过android 自带的android uiau ...