[POJ1338]Ugly Numbers

[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.

输入示例

1
2
9
0

输出示例

1
2
10

数据规模及约定

见“输入

题解

首先与处理一下前 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;
}

 

posted @ 2016-08-24 15:10  xjr01  阅读(199)  评论(0编辑  收藏  举报