UVA11752 The Super Powers【超级幂+暴力+数论】

132 篇文章 1 订阅
61 篇文章 3 订阅

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 = 8^2 and 64 = 4^3. You have to write a program that lists all super powers within 1 and 2^64 − 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

.

.

.


问题链接UVA11752 The Super Powers

问题简述:(略)

问题分析

  根据数论基本定理,任何数均可以分解为素数的乘积。一个数如果分解为x^n,那么超级幂则需要指数是偶数,并且n<=64。

  暴力枚举一下就可以了。

程序说明

  给出两种程序。

题记:(略)

参考链接:(略)


AC的C++语言程序如下:

/* UVA11752 The Super Powers */

#include <iostream>
#include <algorithm>

using namespace std;

typedef unsigned long long ULL;

const int N = 64;
int isprime[N + 1];

bool isPrime(int n)
{
    for(int i=2; i * i <= n; i++)
        if(n % i == 0)
            return false;
    return true;
}

const int N2 = 1e5;
ULL ans[N2 + 1];
const ULL MAX = ((ULL)1 << 64) - 1;

int main()
{
    // Init
    for(int i=1; i<=N; i++)
        isprime[i] = isPrime(i);

    int cnt = 0;
    ans[cnt++] = 0;
    ans[cnt++] = 1;
    
    // 枚举底数
    for(int i=2; i<=N2; i++) {
        ULL r = 1;

        // 枚举指数
        for(int j=1; j<=N; j++) {
            r *= i;
            // 指数为偶数时满足条件
            if(isprime[j] == 0)
                ans[cnt++] = r;

            if(r > MAX / i)
                break;
        }
    }

    sort(ans, ans + cnt);

    for(int i=1; i<cnt; i++)
        if(ans[i] != ans[i - 1])
            cout << ans[i] << endl;

    return 0;
}

AC的C++语言程序(使用set去除重复)如下:

/* UVA11752 The Super Powers */

#include <iostream>
#include <set>
#include <math.h>
#include <stdio.h>
#include <string.h>

//#define DEBUG

using namespace std;

typedef unsigned long long ULL;

const int N2 = 1 << 16;     // sqrt(sqrt(2^64))
const int N = 64;
int vis[N + 1], cn[N + 1], pcn;

void init()
{
    memset(vis, 0, sizeof(vis));

    pcn = 0;
    for(int i=2; i<= N; i++) {
        if(vis[i]) {
            cn[pcn++] = i;
            continue;
        }

        for(int j = i * i; j <= N; j += i)
            vis[j] = 1;
    }
}

void solve()
{
    set<ULL> s;

    s.insert(1);
    // 枚举:x的n次幂
    for(ULL x = 2; x < N2; x++) {
        ULL x4 = x * x * x * x;

        s.insert(x4);

        int end = ceil(64 * log(2) / log(x)) - 1;
        for(int i = 1; cn[i] <= end; i++) {
            x4 *= (cn[i] - cn[i-1] == 1 ? x : x * x);
            s.insert(x4);
        }
    }

    for (set<ULL>::iterator iter = s.begin(); iter != s.end(); iter++)
            printf("%llu\n", *iter);
}

int main()
{
    init();

#ifdef DEBUG
    for(int i=0; i<N; i++)
        printf("%d ", vis[i]);
    printf("\n");
    for(int i=0; i<N; i++)
        printf("%d ", cn[i]);
    printf("\n");
#endif

    solve();

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值