UVA11752-The Super Powers(素数表+log)

题目链接

题意:如果有一个数至少是两个不同的正整数的幂,那么称它为超级幂。按照升序输出1~2^64-1之间的所有超级幂。

思路:n = a ^ i = b ^ j,那么就证明指数要为合数,所以只要找出64以内的所以合数,然后枚举在1 << 16之内的数为底数的值,这里要注意溢出问题,要求出每个底数a的所能得到的最大幂,所以max = (log(2 ^ 64 -1) / log(a));

代码;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>

using namespace std;

typedef unsigned long long ull;

const int MAXN = 100;

int vis[MAXN];

set<ull> arr;
set<ull>::iterator it;

void init() {
    memset(vis, 0, sizeof(vis));
    for (int i = 2; i < MAXN; i++) {
        if (!vis[i])
        for (int j = i * 2; j < MAXN; j += i)
            vis[j] = 1;;
    }
}

ull pow(int n, int k) {
    if (k == 0) return 1;
    if (k == 1) return n;
    ull a = pow(n, k / 2);
    ull ans = a * a;
    if (k % 2)
        ans = ans * n;
    return ans;
}

int main() {
    init();
    arr.clear();
    arr.insert(1);

    double lmax = log(pow(2.0, 64) - 1);
    for (int i = 2; i < (1 << 16); i++) {
        int l = ceil(lmax / log(i));
        for (int j = 4; j < l; j++) {
            if (vis[j]) {
                ull num = pow(i, j);
                if (!arr.count(num))
                    arr.insert(num);
            }
        }
    }

    for (it = arr.begin(); it != arr.end(); it++)
        cout << *it << endl;
    return 0;
}

时间: 2024-08-24 09:03:58

UVA11752-The Super Powers(素数表+log)的相关文章

POJ 1007 Difference Between Primes(线性筛法求N以内的素数表)

Difference Between Primes Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description All you know Goldbach conjecture.That is to say, Every even integer greater than 2 can be expressed as the sum of two primes. Today, sk

利用筛法求素数表

const int Max = 1e6 + 50; int isPrime[Max];<span style="white-space:pre"> </span>//素数判断结果表 int tblPrime[Max];<span style="white-space:pre"> </span>//所求得的素数表 int lenPrimes;<span style="white-space:pre&quo

UVA - 11752 The Super Powers

We all know the Super Powers ofthis world and how they manage to get advantages in political warfare or evenin other sectors. But this is not a political platform and so we will talkabout a different kind of super powers – "The Super Power Numbers&qu

The Super Powers

The Super Powers Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description A The Super Powers   We all know the Super Powers of this world and how they manage to get advantages in political

uva 11752 The Super Powers(暴力)

题目:https://cn.vjudge.net/problem/UVA-11752 题解:这里只讨论处理越界的问题. 因为题目最上界是 264-1. 我们又是求次幂的. 所以当我们就可以知道 i 的时候的界限 limit = 264-1 / i.如果刚好前一个次幂是 limit,那么再乘一个 i 刚好等于 264-1,按照题意是符合的. 那么如果当前的 次幂 a 是大于 limit 的话,a*i 就一定越界(可以自己想一想为什么),这个时候就可以break了. 这一题用set 保存,因为set

筛选法&lt;求素数表&gt;

如果题目的数据规模较大,常规地逐个判断素数的方法行不通,可以使用筛选法进行预处理,将所有素数一次性求出并存入数组中. 筛选法求素数的主要思想如下: (1)将1~N的所有数都标记为素数,0表示素数,1表示非素数. (2)1不是素数,也不是合数,标记为1. (3)2是素数,保留.但比2大的所有2的倍数都标记为1,直到大于N为止. (4)继续寻找素数标记,找到3,将其保留,但比3大的所有3的倍数都标记为,直到大于N为止. -- tip:如果数组声明为全局变量,那么数组会初始化为全0,所以我没有定义in

The Super Powers UVA - 11752(合数幂)

题意: 求1~2^64-1之间所有的 至少是两个不同的正整数的幂的数  升序输出 一个数的合数次幂即为这样的数 找出1~2^64-1中所有数的合数次幂 用set存起来(既能防止重复 又能升序) 最后输出就好了 #include <iostream> #include <cstdio> #include <sstream> #include <cstring> #include <map> #include <set> #include

The Super Powers UVA - 11752

题目大意:将范围从1~pow(2,64)-1内的super power输出.super power的定义:一个数x至少存在两种x=pow(i,k),(k!=1). 题解: 注意数据范围2的64次方-1,而long long 的范围是2的63次方-1,所以要用unsigned long long. 一个数x至少存在两种幂形式,说明这个幂可以拆开,即这个幂不是质数. 最小非质数(1除外)是4,所以我们只需要枚举2的16次方-1就可以了. 指数只需要枚举1~64就可以了.如果指数非质数,就放到集合中.

厄拉多塞筛法和普通方法求素数表(python实现)

厄拉多筛法: 想要得到一个不大于N的数所有素数,可以先找到不超过根号N的所有素数,设2 = p1 < p2 < ......<pk ≤√N,然后在2,3,4......N里面进行下面的操作: 留下p1 = 2,把p1的倍数全部划掉, 再留下p2 ,把p2 的倍数全部划掉, 继续这一过程,直到留下pk,把pk的倍数全部划掉, 最后留下来就是不超过N的全体素数. 举例: N = 30   ,则取pk 为5,所以2到5的所有素数为2,3,5 第一遍 留下2,划去2的所有倍数 2   3