A | The Super Powers |
|
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 81 512 |
寻找1-2的64次幂-1中,可以表示成两个以上不同底数的整数次幂,通过分析可以发现,只要指数是合数就满足条件,因此只要枚举指数和底数即可,由于数较大,所以要用unsigned long long ,运用换底公式,注意向上取整的方法,避免精度损失。
#include<iostream>
#include<cmath>
#include<set>
using namespace std;
#define maxn 90
#define dp(c) ceil(64/(log(c)/log(2)))-1//ceil取整,返回大于等于指定表达式的最小整数,b<(64/(log(a)/log(2))),当后者为整数时,b=(64/(log(a)/log(2)))-1;
//当后者为小数时,b=int((64/(log(a)/log(2)))),即为取整后者向下取整,综上知,b为向上取整在减一
int flag[maxn];
set<unsigned long long >se;//定义容器的存储的数据类型为ull用来存储大数
void intial(){//素数打表
int d=sqrt(90);
for(int i=2;i<=d;i++){
for(int j=i*i;j<=90;j+=i){
if(!flag[j]) flag[j]=1;
}
}
flag[1]=1;
flag[0]=1;
}
unsigned long long fact(int a,int b){//编写计算高幂的函数
unsigned long long sum=1;
for(int i=0;i<b;i++){
sum=sum*a;
}
return sum;
}
int main(){
cout<<1<<endl;
se.clear();
intial();
for( long long i=2;i<fact(2,16);i++){
for(int j=4;j<=dp(i);j++){
if(flag[j]) se.insert(fact(i,j));
}
}
set<unsigned long long >::iterator it=se.begin();
while(it!=se.end()){
cout<<(*it)<<endl;
it++;
}
return 0;
}