uva 11752 The Super Powers (数论+枚举)

题意:找出1~2^64-1中 能写成至少两个数的幂形式的数,再按顺序输出

分析:只有幂是合数的数才是符合要求的。而幂不会超过64,预处理出64以内的合数。

因为最小的合数是4,所以枚举的上限是2的16次方。对其中的每个数以4为幂的枚举下限,并根据合数表递增。而递增的上界是一个数所能达到的最大幂次。可以根据公式:x = logi(2^64-1) = log(2^64-1) / log(i) 得到。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 70010;
typedef long long LL;
typedef unsigned long long ULL;
int tot,v[70],a[70];

void pre()
{
    tot=0;
    memset(v,0,sizeof(v));
    for(int i=2;i<=64;++i){
        if(v[i]){
            a[tot++] = i;
            continue;
        }    
        for(int j=i*2;j<=64;j+=i) v[j]=1;
    }
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    pre();
    set<ULL> res;
    res.insert(1);
    ULL up = (1LL)<<16;
    for(ULL i = 2;i<up;++i){
        int mx = ceil(64*log(2)/log(i))-1;
        ULL tmp = i * i * i * i;
        res.insert(tmp);
        for(int j=1;a[j]<=mx;++j){
            tmp *= (a[j]-a[j-1])==1? i:i*i;
            res.insert(tmp);
        }
    }
    for(auto &v :res){
        printf("%llu
",v);
    }
    return 0;
}
为了更好的明天
原文地址:https://www.cnblogs.com/xiuwenli/p/9480329.html