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;
}