Find All M^N Please

Recently, Joey has special interest in the positive numbers that could be represented as M ^ N (M to the power N), where M and N are both positive integers greater than or equal to 2. For example, 4, 8, 9 and 16 are first four such numbers, as 4 = 2 ^ 2, 8 = 2 ^ 3, 9 = 3 ^ 2, 16 = 2 ^ 4. You are planning to give Joey a surprise by giving him all such numbers less than 2 ^ 31 (2147483648). List them in ascending order, one per line.

Sample Output

4
8
9
16
25
27
32
|
| <-- a lot more numbers
|
1024
1089
1156
1225
1296
1331
|
|
|


找出231以内,不包括231所有可以写成 NM形式的数并按升序输出。
这个题看似数据量巨大,其实并不大,只需要遍历底数和指数就行了。
根据题目要求,最大不超过231,所以最后一个可以写成NM的数一定是sqrt(231),暂且记为lim,进而可以确定底数的范围不会超过1e5。而遍历指数只需要log(n)的时间
具体见代码:


#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>

#define MAXN 46345
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return

#define mem(a,b) memset(a,b,sizeof(a))
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;

bool isPrime[MAXN];
void orgPrime();

vector<int> store;
map<int,bool> mapping;
int main(){
    ios::sync_with_stdio(false);
    int num=pow(2,31);
    int lim=sqrt((double)num);
    rep(i,2,lim+1){
        for(ll j=i*i;j<num;j*=i)
            if(!mapping[j]){
                store.Push(j);
                mapping[j]=true;
            }
    }
    sort(store.begin(),store.end());
    rep(i,0,store.size()){
        cout<<store[i]<<endl;
    }
    re 0;
}
void orgPrime(){
    memset(isPrime,true,sizeof(isPrime));
    isPrime[0]=isPrime[1]=false;
    for(int i=2;i*2<MAXN;i++)
        isPrime[i*2]=false;
    for(int i=3;i<MAXN;i++)
        if(isPrime[i])
            for(int j=3;j*i<MAXN;j+=2)
                isPrime[i*j]=false;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值