HDOJ3208 Integer’s Power
Posted Bombe
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDOJ3208 Integer’s Power相关的知识,希望对你有一定的参考价值。
1. 题目描述
定义如下函数$f(x)$:对于任意整数$y$,找到满足$x^k = y$同时$x$最小并的$k$值。所求为区间$[a, b]$的数代入$f$的累加和,即
\[
\sum_{x=a}^{b} f(x)
\]
2. 基本思路
因为数据很大, 因此不适合暴力枚举。但是对于给定的数$y$,我们可以求得$x^k \le y$。假设$x^k$均不相同,那么直接可解。
因为存在$2^4 = 4^2$的情况,因此,这里其实是个容斥。即$c[i]$的数值应该减掉$c[k \cdot i]$的数值。由于数据一定不超过$2^{63}$,
故对于给定的数$y$,我们可以求得$\{|x| | x^k \le y, k \in [2, 63]\}$然后使用容斥去掉非最优解的情况。
3. 代码
1 /* 3208 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector> 10 #include <deque> 11 #include <bitset> 12 #include <algorithm> 13 #include <cstdio> 14 #include <cmath> 15 #include <ctime> 16 #include <cstring> 17 #include <climits> 18 #include <cctype> 19 #include <cassert> 20 #include <functional> 21 #include <iterator> 22 #include <iomanip> 23 using namespace std; 24 //#pragma comment(linker,"/STACK:102400000,1024000") 25 26 #define sti set<int> 27 #define stpii set<pair<int, int> > 28 #define mpii map<int,int> 29 #define vi vector<int> 30 #define pii pair<int,int> 31 #define vpii vector<pair<int,int> > 32 #define rep(i, a, n) for (int i=a;i<n;++i) 33 #define per(i, a, n) for (int i=n-1;i>=a;--i) 34 #define clr clear 35 #define pb push_back 36 #define mp make_pair 37 #define fir first 38 #define sec second 39 #define all(x) (x).begin(),(x).end() 40 #define SZ(x) ((int)(x).size()) 41 #define lson l, mid, rt<<1 42 #define rson mid+1, r, rt<<1|1 43 44 typedef long long LL; 45 double inf = 1e18 + 400; 46 double bound = 5e18 + 400; 47 LL c[64]; 48 LL a, b; 49 50 LL Pow(LL base, LL n) { 51 LL ret = 1; 52 53 while (n) { 54 if (n & 1) { 55 if (inf/base < ret) return -1; 56 ret = ret * base; 57 } 58 59 n >>= 1; 60 if (bound/base<base && n>0) return -1; 61 base = base * base; 62 } 63 64 return ret; 65 } 66 67 LL gao(LL v, LL n) { 68 LL x = pow((double)v, 1.0/n); 69 LL tmp = Pow(x, n); 70 if (tmp == v) return x; 71 if (tmp>v || tmp==-1) { 72 --x; 73 } else { 74 tmp = Pow(x+1, n); 75 if (tmp!=-1 && tmp<=v) ++x; 76 } 77 78 return x; 79 } 80 81 LL calc(LL n) { 82 int i; 83 84 memset(c, 0, sizeof(c)); 85 c[1] = n; 86 for (i=2; i<64; ++i) { 87 c[i] = gao(n, i) - 1; 88 if (c[i] == 0) break; 89 } 90 91 per(j, 2, i) { 92 rep(k, 1, j) { 93 if (j%k == 0) 94 c[k] -= c[j]; 95 } 96 } 97 98 LL ret = 0; 99 rep(j, 1, i) ret += j*c[j]; 100 return ret; 101 } 102 103 void solve() { 104 LL ans = calc(b) - calc(a-1); 105 printf("%I64d\n", ans); 106 } 107 108 int main() { 109 cin.tie(0); 110 ios::sync_with_stdio(false); 111 #ifndef ONLINE_JUDGE 112 freopen("data.in", "r", stdin); 113 freopen("data.out", "w", stdout); 114 #endif 115 116 while (scanf("%I64d%I64d", &a,&b)!=EOF && (a||b)) { 117 solve(); 118 } 119 120 #ifndef ONLINE_JUDGE 121 printf("time = %ldms.\n", clock()); 122 #endif 123 124 return 0; 125 }
以上是关于HDOJ3208 Integer’s Power的主要内容,如果未能解决你的问题,请参考以下文章
2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!字符串,贪心+排序(示例代码(代
杭电HDOJ--ACM1002(JAVA解题,运用BigInteger)
Check a Integer is power of 3 or not