UVA 11752 The Super Powers(至少是两个正整数的幂的幂数)

题目链接:
UVA 11752 The Super Powers
题意:
求出1~2^64 - 1范围内的所有幂数,并且每个幂数至少是两个正整数的幂。
分析:
要用unsigned long long。
对于任意正整数x它的指数k只要k不是素数,那么x^k必然可以表示成两个不同的正整数的幂。例如512 = 2^9 = (2^3)^3 = 8 ^3
需要知道底数和指数的范围,然后判断指数是不是素数,如果不是素数那就快速幂把结果保存下来。显然底数需要小于1<<16,因为底数至少需要4次方可以,而数据的最大值小于1<<64.对于底数i假设其指数上限为limit则i^limit <= 2^64 - 1,取log得:limit * log(i) < log(2 ^ 64 - 1),所以Limit < log(2 ^ 64 - 1) / log(i),
用上<climits>头文件,2 ^ 64 - 1 = ULONG_LONG_MAX;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#include <bitset>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;

const int MAX_N = 1000010;
const ull INF = ULONG_LONG_MAX;

int cnt = 0;
ull ans[MAX_N];
bitset<MAX_N> bs;

void GetPrime()
{
    bs.set();
    for(int i = 2; i < MAX_N; i++){
        for(int j = 2 * i; j < MAX_N; j += i){
            bs[j] = 0;
        }
    }
}

ull quick_pow(ull n, ull m)
{
    ll res = 1, tmp = n;
    while(m){
        if( m & 1) res = res * tmp;
        tmp = tmp * tmp;
        m >>= 1;
    }
    return res;
}

int main()
{
    GetPrime();
    ans[cnt++] = 1;
    int len = (1 << 16);
    double Max = log(1.0 * INF);
    for(int i = 2; i < len; i++){
        int limit = (int)ceil(Max / log(i * 1.0));
        for(int j = 4; j < limit; j++){
            if(bs[j] == 0) ans[cnt++] = quick_pow(i, j);
        }
    }
    sort(ans, ans + cnt);
    int total = unique(ans, ans + cnt) - ans;
    for(int i = 0; i < total; i++){
        printf("%llu\n", ans[i]);
    }
    //printf("cnt = %d\n", cnt);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值