[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. 【BZOJ1011】【HNOI2008】遥远的行星(乱搞)

    1011: [HNOI2008]遥远的行星 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 1444  Solved ...

  2. [JQuery EasyUI系列]简介

    一.jQuery EasyUI是一个基于jQuery的框架,继承了各种用户界面插件. 二.jQuery EasyUI框架提供了创建网页所需的一切,可以轻松建立站点. easyui是一个基于jQuery ...

  3. “耐撕”团队记账本 剧透

    β发布之后,我们团队开始fork"OneZero"团队的记账本程序.我们在原来的基础上添加了以下功能: 下面是我们团队记账本程序演示的视频:http://v.youku.com/v ...

  4. 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.jee.server:CurrencyClientServe

    有的说: 在Servers视图里双击创建的server,然后在其server的配置界面中选中"Publish module contexts to separate XML files&qu ...

  5. Java-transient

    transient的作用及使用方法 都知道一个对象只要实现了Serilizable接口,这个对象就可以被序列化,java的这种序列化模式为开发者提供了很多便利,我们可以不必关系具体序列化的过程,只要这 ...

  6. yii2图片上传

    yii2利用自带UploadedFile上传图片 public static function uploadFile($name) { $uploadedFile = UploadedFile::ge ...

  7. linux 问答

    问:1 如何查看当前的Linux服务器的运行级别? 答: ‘who -r’ 和 ‘runlevel’ 命令可以用来查看当前的Linux服务器的运行级别. 问:2 如何查看Linux的默认网关? 答: ...

  8. 编译rnnlib

    rnnlib,一个多年不更新的rnn库,编译的过程有点麻烦,好多东西要选特定版本的.这里记录一下我的配置脚本,在ubuntu14.04下测试ok. P.S fedora下好像不能直接用包管理来安装指定 ...

  9. BZOJ-1901 Zju2112 Dynamic Rankings 函数式线段树 套 树状数组+离线处理

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MB Submit: 6058 Solved: 2521 [Su ...

  10. 一个项目中哪些文件是要上传到 git上的,哪些是不必要的