UVA 11752 The Super Powers

本文介绍了一种算法,用于找出1到2^64-1范围内的所有超级幂次数,即那些至少可以由两个不同正整数的幂表示的正整数。文章提供了详细的解题思路和C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }
View Code

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值