[POJ1338]Ugly Numbers

试题描述

Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 
1, 2, 3, 4, 5, 6, 8, 9, 10, 12, ... 
shows the first 10 ugly numbers. By convention, 1 is included. 
Given the integer n,write a program to find and print the n'th ugly number.

输入

Each line of the input contains a postisive integer n (n <= 1500).Input is terminated by a line with n=0.

输出

For each line, output the n’th ugly number .:Don’t deal with the line with n=0.

输入示例


输出示例


数据规模及约定

见“输入

题解

首先与处理一下前 1500 个“丑数”,建立一个堆,对于一个“丑数” x,我们把 x * 2, x * 3, x * 5 都扔进堆,输出一下发现居然没有爆 unsigned long long(事实上连 int 都没爆),所以就是思博题了。

查询就访问数组即可。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; const int BufferSize = 1 << 16;
char buffer[BufferSize], *Head, *Tail;
inline char Getchar() {
if(Head == Tail) {
int l = fread(buffer, 1, BufferSize, stdin);
Tail = (Head = buffer) + l;
}
return *Head++;
}
int read() {
int x = 0, f = 1; char c = Getchar();
while(!isdigit(c)){ if(c == '-') f = -1; c = Getchar(); }
while(isdigit(c)){ x = x * 10 + c - '0'; c = Getchar(); }
return x * f;
} #define maxn 1510
#define ULL unsigned long long
int n;
ULL num[maxn];
priority_queue <ULL> Q; int main() {
num[1] = 1;
Q.push(-2ll); Q.push(-3ll); Q.push(-5ll);
for(int i = 2; i <= 1500; i++) {
num[i] = -Q.top(); Q.pop();
while(num[i] == num[i-1]) num[i] = -Q.top(), Q.pop();
Q.push(-(num[i] << 1ll)); Q.push(-num[i] * 3ll); Q.push(-num[i] * 5ll);
} while(1) {
n = read();
if(!n) break;
printf("%llu\n", num[n]);
} return 0;
}

[POJ1338]Ugly Numbers的更多相关文章

  1. poj1338 Ugly Numbers 打表, 递推

    题意:一个数的质因子能是2, 3, 5, 那么这个数是丑数. 思路: 打表或者递推. 打表: 若该数为丑数,那么一定能被2 或者3, 或者5 整除, 除完之后则为1. #include <ios ...

  2. Ugly Numbers

    Ugly Numbers Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21918 Accepted: 9788 Descrip ...

  3. poj 1338 Ugly Numbers(丑数模拟)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063? viewmode=contents 题目链接:id=1338&q ...

  4. leetcode@ [263/264] Ugly Numbers & Ugly Number II

    https://leetcode.com/problems/ugly-number/ Write a program to check whether a given number is an ugl ...

  5. Geeks Interview Question: Ugly Numbers

    Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence1, 2, 3, 4, 5, 6, 8, 9, ...

  6. 136 - Ugly Numbers

     Ugly Numbers  Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3 ...

  7. Ugly Numbers(STL应用)

    题目链接:http://poj.org/problem?id=1338 Ugly Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

  8. 丑数(Ugly Numbers, UVa 136)

    丑数(Ugly Numbers, UVa 136) 题目描述 我们把只包含因子2.3和5的数称作丑数(Ugly Number).求按从小到大的顺序的第1500个丑数.例如6.8都是丑数,但14不是,因 ...

  9. UVA.136 Ugly Numbers (优先队列)

    UVA.136 Ugly Numbers (优先队列) 题意分析 如果一个数字是2,3,5的倍数,那么他就叫做丑数,规定1也是丑数,现在求解第1500个丑数是多少. 既然某数字2,3,5倍均是丑数,且 ...

随机推荐

  1. 文件夹文件遍历并插入数据库的操作,IO Directory File的递归操作

    在我们管理内容管理系统时,数据量大时,对机器的依赖性就比较强了,比如,我要将一个文件夹中的很多图片上传到网站,一个个上传会很花时间,就想到了通过遍历文件夹得到文件名,并将路径与文件保存到数据库中对应的 ...

  2. Scala 中的函数式编程基础(一)

    主要来自 Scala 语言发明人 Martin Odersky 教授的 Coursera 课程 <Functional Programming Principles in Scala>. ...

  3. Mysql-proxy中的lua脚本编程(一)

    在为mysql-proxy编写lua脚步的时候,需要知道一下几个入口函数,通过这几个入口函数我们可以控制mysql-proxy的一些行为. connect_server()          当代理服 ...

  4. Memcached——分布式缓存

    下载文件:https://sourceforge.net/projects/memcacheddotnet/ 将Commons.dll,ICSharpCode.SharpZipLib.dll,log4 ...

  5. GCD 深入理解:第二部分

    在本系列的第一部分中,你已经学到超过你想像的关于并发.线程以及GCD 如何工作的知识.通过在初始化时利用 dispatch_once,你创建了一个线程安全的 PhotoManager 单例,而且你通过 ...

  6. HTML DOM 基础

    $. HTML DOM 定义了访问和操作 HTML 文档的标准方法.  DOM 是 W3C(万维网联盟)的标准. $. DOM树. $. W3C 文档对象模型 (DOM) 是中立于平台和语言的接口,它 ...

  7. java5中原子型操作类的应用

    java.util.concurrent.atomic包中提供了对基本数据类型,对数组中的基本数据类型和类中的基本数据类型的操作.详情见API. 下面实例简单介绍AtomicInteger类的使用: ...

  8. Java-list,set,map的区别

    jdk中api的定义 Collection ├----List │ ├----LinkedList │ ├----ArrayList │ └----Vector │ └----Stack └----S ...

  9. Java基础-gs(垃圾回收)

    Java垃圾回收概况 Java GC(Garbage Collection,垃圾收集,垃圾回收)机制,是Java与C++/C的主要区别之一,作为Java开发者,一般不需要专门编写内存回收和垃圾清理代 ...

  10. 【Matplotlib】设置刻度(1)

    刻度设置 参考文档: xticks 命令 yticks 命令 以xticks为例: matplotlib.pyplot.xticks(*args, **kwargs) 获取或者设置当前刻度位置和文本的 ...