码迷,mamicode.com
首页 > 其他好文 > 详细

The Super Powers UVA - 11752(合数幂)

时间:2018-07-16 18:29:12      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:double   mat   clu   insert   题意   整数   tac   之间   string   

题意:

求1~2^64-1之间所有的 至少是两个不同的正整数的幂的数  升序输出 

一个数的合数次幂即为这样的数

找出1~2^64-1中所有数的合数次幂 用set存起来(既能防止重复 又能升序) 最后输出就好了

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <climits>
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 100010, INF = 0x7fffffff;
LL vis[maxn];
int ans;
set<ULL> s;

ULL power(ULL a, ULL b) {     //快速幂
    ULL res = 1;
    while(b)
    {
        if(b & 1) res = res * a;
        a = a * a;
        b >>= 1;
    }
    return res;
}
//ULL power(int a, int b)  //二分幂
//{
//    if(b == 0) return 1;
//    ULL res = power(a, b/2);
//    res *= res;
//    if(b & 1) res *= a;
//    return res;
//}

void init()
{
    ans = 0;
    mem(vis, 0);
    for(int i=2; i<=64; i++)
        if(!vis[i])
        {
            for(LL j=(LL)i*i; j<=64; j+=i)
               vis[j] = 1;
        }
}

int main()
{
    init();
    s.insert(1);

    for(ULL i=2; i< (1<<16); i++)
    {
        double t=ceil(64.0/log(i)*log(2))-1;

        for(int j=4; j<=64; j++)
        {
            if(!vis[j])continue;
            if(j> t) break;
            ULL res = power(i, j);
            s.insert(res);
        }
    }
    for(set<ULL>::iterator it=s.begin(); it!=s.end(); it++)
        printf("%llu\n",*it);


    return 0;
}

 

The Super Powers UVA - 11752(合数幂)

标签:double   mat   clu   insert   题意   整数   tac   之间   string   

原文地址:https://www.cnblogs.com/WTSRUVF/p/9319092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!