UVA-11752 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 Output

1

16

64

81

256

512

.

.

.

 

题意:求超级幂。一个数n能表示成ai^bi,a,b至少存在两组时,n为超级幂。输出2^64-1内的所有超级幂。

解析:底数在[2,1<<16]内,指数是64以内的合数。底数为a的幂不会溢出的上限为ceil(64/((log(a)/log(2)))-1。

一开始以为底数应该是素数,导致wa了很多次。

 

代码如下:

# include<iostream>
# include<cstdio>
# include<cstring>
# include<set>
# include<vector>
# include<cmath>
# include<algorithm>
using namespace std;
const int N=105;
int vis[N];
unsigned long long mypow(int a,int b)
{
    if(b==0)
        return 1;
    if(b==1)
        return a;
    unsigned long long res=mypow(a,b>>1);
    res*=res;
    if(b&1)
        res*=a;
    return res;
}
void get_prim()
{
    int i,j;
    fill(vis,vis+N,0);
    for(i=2;i<N;++i){
        if(vis[i])
            continue;
        for(j=i+i;j<N;j+=i)
            vis[j]=1;
    }
}
int main()
{
    int i,j;
    get_prim();
    set<unsigned long long>s;
    s.insert(1);
    set<unsigned long long>::iterator it;
    for(i=2;i<(1<<16);++i){
        for(j=4;j<=ceil(64/(log(i)/log(2)))-1;++j){
            if(vis[j]){
                s.insert(mypow(i,j));
            }
        }
    }
    for(it=s.begin();it!=s.end();++it)
        printf("%llu\n",*it);
    return 0;
}

 

posted @ 2015-07-22 20:13  20143605  阅读(342)  评论(0编辑  收藏  举报