uva11752(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,264-1]的所有超级幂。如果一个数是超级幂,
那么它能表示成至少两个不同数的幂形式。
容易知道只有幂是合数才能拆分,这段区间中最小的数为2(1为特殊情况),最大的幂为64,
所以把1到64所有的合数选出来,再枚举底数,因为最小的合数为4,所以最大的底数为216 ,
因此枚举2~216 的底数就可以了,注意先用对数判断一个数是否大于264-1就可以了。
#include<cstdio> #include<algorithm> #include<cmath> double M=log10(18446744073709551615.0); using namespace std; int e[45]={4,6,8,9,10,12,14,15,16,18,20,21,22,24,25,26,27,28,30,32,33,34,35,36,38,39,40,42,44,45,46,48,49,50,51,52,54,55,56,57,58,60,62,63,64}; unsigned long long Pow(unsigned long long x,int k) { if(k==1) return x; unsigned long long y=Pow(x,k/2); y*=y; if(k%2) y*=x; return y; } unsigned long long ans[100000]; int main() { int N=1; for(int i=2;i<65536;i++) { int flag=1; for(int j=0;j<45;j++) { if((double)(e[j]*1.0)*log10(i)>M) break; unsigned long long temp=Pow(i,e[j]); ans[N++]=temp; flag=0; } if(flag) break; } printf("1\n"); sort(ans+1,ans+N); for(int i=1;i<N;i++) if(ans[i]!=ans[i-1]) printf("%llu\n",ans[i]); return 0; }
作者:ACRykl —— O ever youthful,O ever weeping!
出处:http://www.cnblogs.com/ACRykl/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。