zoukankan      html  css  js  c++  java
  • UVa 11752 (素数筛选 快速幂) The Super Powers

    首先有个关键性的结论就是一个数的合数幂就是超级幂。

    最小的合数是4,所以枚举底数的上限是pow(2^64, 1/4) = 2^16 = 65536

    对于底数base,指数的上限就是ceil(64*log(2)/log(base)),注意这个上限不能取到,是个开区间

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <set>
     4 #include <cassert>
     5 using namespace std;
     6 
     7 typedef unsigned long long ULL;
     8 typedef set<ULL>::iterator It;
     9 bool vis[70];
    10 
    11 ULL POW(int a, int p)
    12 {
    13     ULL ans = 1, base = a;
    14     while(p)
    15     {
    16         if(p & 1ULL) ans *= base;
    17         base *= base;
    18         p >>= 1;
    19     }
    20     return ans;
    21 }
    22 
    23 int main()
    24 {
    25     for(int i = 2; i <= 64; i++) if(!vis[i])
    26         for(int j = i * i; j <= 64; j += i) vis[j] = true;
    27 
    28     set<ULL> ans;
    29     ans.insert(1);
    30     for(int base = 2; base < 65536; base++)
    31         for(int p = 4; p < (int)ceil(64.0 * log(2.0) / log(base)); p++) if(vis[p])
    32         {
    33             ULL v = POW(base, p);
    34             assert(v != 1ULL); assert(v != 0);
    35             if(!ans.count(v)) ans.insert(v);
    36         }
    37 
    38     for(It i = ans.begin(); i != ans.end(); i++)
    39         printf("%llu
    ", *i);
    40 
    41 
    42     return 0;
    43 }
    代码君
  • 相关阅读:
    三角形的个数
    Nightmare(搜索)
    Prime Ring Problem(搜索)
    Safecracker(搜索)
    丑数
    八皇后问题(回溯法)
    Dijkstra
    floyd详解
    继续畅通工程(kruskal prim)
    畅通工程
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/4357304.html
Copyright © 2011-2022 走看看