UVA 11752 The Super Powers(暴力)

题目:https://cn.vjudge.net/problem/UVA-11752

题解:这里只讨论处理越界的问题。

   因为题目最上界是 264-1。 我们又是求次幂的。

   所以当我们就可以知道 i 的时候的界限 limit = 264-1 / i。如果刚好前一个次幂是 limit,那么再乘一个 i 刚好等于 264-1,按照题意是符合的。

   那么如果当前的 次幂 a 是大于 limit 的话,a*i 就一定越界(可以自己想一想为什么),这个时候就可以break了。

  这一题用set 保存,因为set中不会出现重复元素,而且元素是从小到大的顺序排列的。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 #include <stack>
11 #include <set>
12 using namespace std;
13 typedef long long LL;
14 typedef unsigned long long uLL;
15 #define ms(a, b) memset(a, b, sizeof(a))
16 #define pb push_back
17 #define mp make_pair
18 const LL INF = 0x7fffffff;
19 const int inf = 0x3f3f3f3f;
20 const int mod = 1e9+7;
21 const int maxn = 100000+10;
22 const int maxm = 1000000+10;
23 bool heshu[maxn];
24 set<uLL> ans;
25 int main() {
26 #ifdef LOCAL
27     freopen("input.txt", "r", stdin);
28 //        freopen("output.txt", "w", stdout);
29 #endif
30 //    ios::sync_with_stdio(0);
31 //    cin.tie(0);
32     ms(heshu, false);
33     for(int i = 2;2*i<100;i++){
34         for(int j = 2;i*j<100;j++)
35             heshu[i*j]=true;
36     }
37     uLL top = (1<<64)-1uLL;
38     for(uLL i = 2uLL;i<65536uLL;i++){
39         uLL limit = top / i;
40         uLL now = 1uLL*i*i*i*i;
41         for(int j = 4;j<64;j++){
42             if(heshu[j])
43                ans.insert(now);
44             if(now>limit)   break;
45             now*=i;
46         }
47     }
48     ans.insert(1uLL);
49     set<uLL>::iterator q;
50     for(q = ans.begin();q!=ans.end();q++)
51         cout << *q << endl;
52 //    cout << ans.size() << endl;
53 }
View Code
原文地址:https://www.cnblogs.com/denghaiquan/p/7231206.html