首页 > 代码库 > UVA 11752 The Super Powers
UVA 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
.
.
.
解题思路:
直接扫描2~(2^16-1)内的所有数字,设为i;设指数为j,令j=4,检查所有大于等于4,并且使i^j<=(2^64-1)成立的j。
i^j<=(2^64-1)可化为j<=ln(2^64-1)/lni。(为了防爆long long,可以用double)
这有有个需要注意的地方。double可以表示1.7* 10的300次方,但不能准确表示1.7* 10的300次方加上1那个数。按科学计算法书写,double可以有15位有效数字。
所以2^64-1并不能用double精确表示。可以用ln(2^64-1)减小误差。(只能减小,不能完全消除)
1 #include <cstdio> 2 #include <cmath> 3 #include <set> 4 using namespace std; 5 #define ll unsigned long long 6 7 set<ll> se; 8 9 int OK(int x) 10 { 11 for(int i=2;i<=x/2;i++) 12 if(x%i==0) 13 return 1; 14 return 0; 15 } 16 17 ll poww(ll x,int n) 18 { 19 ll ans=1; 20 while(n) 21 { 22 if(n&1) 23 ans*=x; 24 x*=x; 25 n/=2; 26 } 27 return ans; 28 } 29 30 int main() 31 { 32 double s= pow(2.0, 64.0)-1 ; 33 se.insert(1); 34 for(int i=2;i<(1<<16);i++) 35 { 36 for(int j=4;j<(int)ceil(log(s)/log(i*1.0));j++) 37 { 38 if(OK(j))//j不超过63 39 se.insert(poww((ll)i,j)); 40 } 41 } 42 43 for(set<ll>::iterator it=se.begin();it!=se.end();it++) 44 printf("%llu\n",*it); 45 return 0; 46 }
UVA 11752 The Super Powers