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 264 − 1. Each line contains a single super power
number and the numbers are printed in ascending order.
Note: Remember that there are no input for this problem. The sample output is only a partial solution.
Sample Input
Sample Output
1
16
64
81
256
512
.
.
.
题解:
其实通过观察发现。一个数的合数次幂是符合要求的数,我们先求出64之前的所有合数,之后枚举一下。注意i的范围是2^16次方,注意剪掉,不要超过2^64次方。计算完后插入一个1.set去重,自动排序,非常好用。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
using namespace std;
const int MAXN = 100;
bool notprime[MAXN];
typedef unsigned long long LL;
LL INF = (1<<64) - 1;
set<LL> s;
void init()
{
memset(notprime,false,sizeof(notprime));
notprime[0]=notprime[1]=false;
for(int i=2;i<MAXN;i++)
{
if(!notprime[i])
{
if(i>MAXN/i)
{
continue;
}
for(int j=i*i;j<MAXN;j+=i)
{
notprime[j]=true;
}
}
}
}
LL power(int a, int n) {
if(n == 0) return 1LL;
LL ans = power(a, n/2);
ans *= ans;
if(n%2 == 1) ans *= a;
return ans;
}
int main()
{
init();
for(int i=2;i<=65536;i++)
{
LL ans=1;
for(int j=1;j<=64;j++)
{
ans*=i;
if(notprime[j]) s.insert(ans);
if(ans>INF/i) break;
}
}
s.insert(1);
set<LL>::iterator iter;
for(iter=s.begin();iter!=s.end();iter++)
{
cout<<*iter<<endl;
}
return 0;
}