11752 - The Super Powers

Time limit: 1.000 seconds

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=467&page=show_problem&problem=2852

We all know the Super Powers of this world and how they manage to get advantages in political warfare or even in other sectors. But this is not a political platform and so we will talk about a different kind of super powers – “The Super Power Numbers”. A positive number is said to be super power when it is the power of at least two different positive integers. For example 64 is a super power as 64 = 82 and 64 =  43. You have to write a program that lists all super powers within 1 and 264 -1 (inclusive).  

Input
This program has no input.

Output

Print all the Super Power Numbers within 1 and 2^64 -1. Each line contains a single super power number and the numbers are printed in ascending order. 

Sample Input  

Partial Judge Output

No input for this problem   

1

16
64

81
256

512
.
.
.


用set实现,也可以用数组和sort&unique实现(更快)


完整代码:

/*0.036s*/

#include<cstdio>
#include<cmath>
#include<set>
#define LL unsigned long long
using namespace std;
const int index[100] =
{
	4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26,
	27, 28, 30, 32, 33, 34, 35, 36, 38, 39, 40, 42, 44, 45, 46,
	48, 49, 50, 51, 52, 54, 55, 56, 57, 58, 60, 62, 63, 64  /// 多算一个
};///合数表

set<LL> ans;

int main(void)
{
	LL i, maxi = 1 << 16; /// 底数的上限
	int j, maxindex; /// 指数的上限
	LL cur;
	ans.insert(1);
	for (i = 2; i < maxi; i++)
	{
		cur = i * i; /// i用LL类型的原因
		cur *= cur;
		ans.insert(cur);
		maxindex = ceil(64 * log(2) / log(i)) - 1; /// 指数的上限
		for (j = 1; index[j] <= maxindex; ++j)
		{
			cur *= (index[j] - index[j - 1] == 1 ? i : i * i);
			ans.insert(cur);
		}
	}
	for (set<LL>::iterator iter = ans.begin(); iter != ans.end(); ++iter)
		printf("%llu\n", *iter);
	return 0;
}