首页 > 代码库 > UVa11752
UVa11752
11752 The Super Powers
We all know the Super Powers of this world and how they manage to get advantages in political warfare
or even in other sectors. But this is not a political platform and so we will talk about a different kind
of super powers — “The Super Power Numbers”. A positive number is said to be super power when it
is the power of at least two different positive integers. For example 64 is a super power as 64 = 82 and
64 = 43. You have to write a program that lists all super powers within 1 and 264 ?? 1 (inclusive).
Input
This program has no input.
Output
Print all the Super Power Numbers within 1 and 264 ?? 1. Each line contains a single super power
number and the numbers are printed in ascending order.
Note: Remember that there are no input for this problem. The sample output is only a partial solution.
Sample Input
Sample Output
1
16
64
81
256
512
...
题意:
超级幂就是指一个数至少是两个不同正整数的幂。找出1~2^64-1的所有超级幂,按照升序输出。
分析:
注意到超级幂可以写成一个正整数的合数次幂。所以思路就是枚举一定范围的正整数的合数次幂。注意到整数的范围是Unsigned long long,所以需要事先计算出每一个底数所能枚举的极限幂次,这也很容易,只需要令lim=2^64-1,然后不断用某个底数去除lim就可以得到该底数的极限幂次。当然底数也只需要枚举有限个,当底数的3次幂大于了lim就停止枚举底数。
1 #include<iostream> 2 #include<set> 3 #include<cstdio> 4 using namespace std; 5 typedef unsigned long long ull; 6 int primes[] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61}; 7 int vis[70]; 8 int main(){ 9 ull lim = ~0LL >> 1;//最大边界10 set<ull> s;11 for(int i = 0; i < 18; i++) vis[primes[i]] = 1;//标记素数12 for (ull i = 2;; i++){//枚举底数13 ull cnt = -1, x = lim;14 while(x) //计算出最大的指数cnt15 x /= i,cnt++;16 if(cnt < 4) break;17 ull b = i;18 for (ull j = 2 ; j <= cnt ; j++){19 b *= i;20 if (!vis[j])//枚举指数,如果该指数是合数,就放入set21 s.insert(b);22 }23 }24 s.insert(1);//1是一个特例,最后放入25 set<ull>::iterator it;26 for(it = s.begin() ; it != s.end() ; it++)27 cout << *it << ‘\n‘;28 return 0;29 }
UVa11752