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 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值