Uva 11752 - The Super Powers

// Accepted

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<vector>
#include<bitset>
#include<set>

#define limit 90
#define high 18446744073709551615
using namespace std;

typedef unsigned long long ull;
typedef long long LL;
typedef vector<LL>vll;
typedef set<ull>sull;

bool flag[limit+5];

void sieve()
{
    memset(flag, false, sizeof(flag));

    int i,j;

    for(i=4; i<=limit; i+=2)
    {
        flag[i] = true;
    }

    for(i=3 ; i*i<=limit; i+=2)
    {
        if(!flag[i])
        {
            for(j=i*i;j<=limit;j+=(2*i))
            {
                //setbit(j);
                flag[j] = true;
            }
        }
    }

//    for(i=0;i<101;i++)
//    {
//        if(flag[i])
//        {
//            cout << i << "; ";
//        }
//    }
}

ull mpower(ull n, int p)
{
    ull ret = 1;

    for(int i=1; i<=p; i++)
    {
        if(ret > (high / n))
        {
            ret = 0;
            break;
        }

        else
        {
            ret *= n;
        }
    }

    return ret;
}

sull st;

void SuperPowers()
{
    ull supr;

    for(ull i=2; i<=100000; i++)
    {
        for(int j=4; j<=64; j++)
        {
            if(flag[j])
            {
                supr = mpower(i,j); //cout << supr << "; ";

                st.insert(supr);
            }
        }
    }

    cout << "1\n";

    for(sull::iterator it=st.begin(); it!=st.end();it++)
    {
        if(*it != 0)
        {
            cout << *it << "\n";
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    //freopen("out.txt", "w", stdout);
    sieve();
    SuperPowers();
    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number