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
.
.
.
原文:https://blog.csdn.net/Big_Rui/article/details/77417089
【题意】:
超级数能化成两个不同的数的平方数, 例如16=2^4=4^2;
求2^64-1以内的超级数;
思路:关键一点就是任意数ans=x^n,如果n是合数,则ans是 超级数,
接下来就是枚举,很明显判定终点x^n会爆long long
所以转换成直接判定指数 i^j<2^64-1 == > j<ln(2^64-1)/ln (i),这样指数j从第一个合数4循环到 ln(2^64-1)/ln (i)
还有一点:一定要用unsign long long,不然超过2^63-1,会爆导致数据丢失(第一位会舍弃)
unsign long long 数据范围是0-2^64-1
long long 数据范围是-2^63到+2^63-1
【小结】:
以上都是通过看别人博客找到的,写博客的原因主要是两个,
第一运用C语言里面的log对数函数的使用,
首先C语言里面内置了两个log函数:
1、log()函数----ln(x) - 以e为底, x为对数。
2、log10()函数-----lg(x) - 以10为底,x为对数。
这个题我错了好几遍,主要是:
第一、没有看清楚 指数必须是要合数,不能是质数。(欧拉筛不熟,还写错了一点)
第二、原来精度出问题,是这么痛苦,log( double ) 里面必须是double 不然会精度损失。
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int M=1e6+10;
int x[M],prime[M];
void is_prime(int n){
int tot=0;
for(int i=2;i<=n;i++){
if(x[i]==0){
prime[tot++]=i;
}
for(int j=0;j<tot&&i*prime[j]<=n;j++){
x[i*prime[j]]=true;
if(i%prime[j]==0){
break;
}
}
}
}
ull qpow(ull a, ull b){
ull ans=1;
while(b){
if(b&1){
ans=a*ans;
}
a=a*a;
b>>=1;
}
return ans;
}
set<ull>S;
int main()
{
S.clear();
is_prime(100);
S.insert(1);
/*for(int i=0;i<10;i++){
printf("%d\n",prime[i]);
}*/
double N=(pow(2.0,64)-1);
for(ull i=2;i<=(1ll<<16);i++){
ull num=(int)ceil(log(N)/log(i*1.0));
for(ull j=4 ; j<num ;j++) {
ull tmp=qpow(i,j);
//cout<<tmp<<endl;
if(x[j]==true)
S.insert(tmp);
}
}
set<ull>::iterator it;
int b=0;
for(it=S.begin();it!=S.end();it++){
//b++;
printf("%llu\n",*it);
/*if(b==100){
break;
}*/
}
return 0;
}