UVA-11752 The Super Powers

本文介绍了一种算法,用于找出在1到2^64-1范围内的所有超级幂数。超级幂数是指能够表示为至少两种不同正整数的幂的正数。文章详细解释了算法思路,并给出了具体实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}

 

转载于:https://www.cnblogs.com/20143605--pcx/p/4668501.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值