1. 程式人生 > >【數論】The Super Powers (英語+log函式使用)

【數論】The Super Powers (英語+log函式使用)

題目連結:

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;
}