UVA 11752 The Super Powers(至少是两个正整数的幂的幂数)
来源:互联网 发布:啊哈算法 在线阅读 编辑:程序博客网 时间:2024/06/06 01:06
题目链接:
UVA 11752 The Super Powers
题意:
求出1~2^64 - 1范围内的所有幂数,并且每个幂数至少是两个正整数的幂。
分析:
要用unsigned long long。
对于任意正整数x它的指数k只要k不是素数,那么x^k必然可以表示成两个不同的正整数的幂。例如512 = 2^9 = (2^3)^3 = 8 ^3
需要知道底数和指数的范围,然后判断指数是不是素数,如果不是素数那就快速幂把结果保存下来。显然底数需要小于1<<16,因为底数至少需要4次方可以,而数据的最大值小于1<<64.对于底数i假设其指数上限为limit则i^limit <= 2^64 - 1,取log得:limit * log(i) < log(2 ^ 64 - 1),所以Limit < log(2 ^ 64 - 1) / log(i),
用上<climits>
头文件,2 ^ 64 - 1 = ULONG_LONG_MAX
;
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <climits>#include <cmath>#include <ctime>#include <cassert>#include <bitset>#define IOS ios_base::sync_with_stdio(0); cin.tie(0);using namespace std;typedef long long ll;typedef unsigned long long ull;const int MAX_N = 1000010;const ull INF = ULONG_LONG_MAX;int cnt = 0;ull ans[MAX_N];bitset<MAX_N> bs;void GetPrime(){ bs.set(); for(int i = 2; i < MAX_N; i++){ for(int j = 2 * i; j < MAX_N; j += i){ bs[j] = 0; } }}ull quick_pow(ull n, ull m){ ll res = 1, tmp = n; while(m){ if( m & 1) res = res * tmp; tmp = tmp * tmp; m >>= 1; } return res;}int main(){ GetPrime(); ans[cnt++] = 1; int len = (1 << 16); double Max = log(1.0 * INF); for(int i = 2; i < len; i++){ int limit = (int)ceil(Max / log(i * 1.0)); for(int j = 4; j < limit; j++){ if(bs[j] == 0) ans[cnt++] = quick_pow(i, j); } } sort(ans, ans + cnt); int total = unique(ans, ans + cnt) - ans; for(int i = 0; i < total; i++){ printf("%llu\n", ans[i]); } //printf("cnt = %d\n", cnt); return 0;}
0 0
- UVA 11752 The Super Powers(至少是两个正整数的幂的幂数)
- UVA 11752 The Super Powers 超级幂
- UVA 11752 The Super Powers【超级幂】
- UVA 11752 The Super Powers —— 数学与幂
- UVA - 11752 The Super Powers
- UVA 11752-The Super Powers
- Uva 11752 The Super Powers
- UVA 11752 The Super Powers
- uva 11752The Super Powers
- UVA 11752 The Super Powers
- uva 11752 The Super Powers
- UVA 11752 The Super Powers
- The Super Powers UVA
- The Super Powers UVA
- The Super Powers UVA
- UVa 11752 The Super Powers (数学)
- UVA 11752 - The Super Powers (数论)
- UVa 11752 - The Super Powers (数论)
- redis事件
- VideoView播放视频异常MediaPlayer: Error (1,-2147483648)
- Python学习 (九 正则表达式)
- UIResponder
- Zookeeper的安装配置及基本开发
- UVA 11752 The Super Powers(至少是两个正整数的幂的幂数)
- 构造方法
- zznu 1203: 做幻方
- SQL SERVER 2005命令行修改用户sa密码
- 一个新的开始,纪录点点滴滴
- 软件开发平台实现目标归纳
- IOS手势处理
- php 实现文件上传功能
- [原创翻译]《Netty in Action》英文2016年版中文翻译版·第二章:你的第一个Netty应用程序