题解 UVa11752

题目大意 请求出所有小于 \(2^{64}-1\) 的正整数 \(n\), 使得 \(\exists p, q, a, b\in \mathbb{N+}, p\neq q\rightarrow a^p=b^q=n\)

分析 不难发现,\(\forall n\) 满足条件, \(\exists r\in\mathbb{N+}\rightarrow n=r^{pq}\)\(pq\) 为不超过 \(64\) 的合数。所以预处理指数,再枚举 \(r\) 即可。

#include<bits/stdc++.h>
using namespace std;

typedef unsigned long long ull; 
const ull maxnum = ~0ull;

int tot;
ull ans[10000000];
int cnt, prime[70];
bool nprime[70];

void GetPrime(int n)
{
    for(int i = 2; i <= n; ++i) {
        if(!nprime[i]) prime[++cnt] = i;
        for(int j = 1; j <= cnt && i * prime[j] <= n; ++j) {
            nprime[i * prime[j]] = 1;
            if(i % prime[j] == 0) break;
        }
    }
}

int main()
{
    GetPrime(64);
    
    for(ull i = 1; i <= 65536; ++i) {
        ull base = 1;
        for(int j = 1; j <= 64; ++j) {
            if(maxnum / i >= base) {
                base *= i;
                if(nprime[j]) ans[++tot] = base;
            }
        }
    }

    sort(ans + 1, ans + tot + 1);
    tot = unique(ans + 1, ans + tot + 1) - ans - 1;
    
    for(int i = 1; i <= tot; ++i)
        printf("%llu\n", ans[i]);
}

猜你喜欢

转载自www.cnblogs.com/whx1003/p/11961172.html
今日推荐