【HDOJ】3208 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. 代码
/* 3208 */
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
#include <deque>
#include <bitset>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <climits>
#include <cctype>
#include <cassert>
#include <functional>
#include <iterator>
#include <iomanip>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,1024000") #define sti set<int>
#define stpii set<pair<int, int> >
#define mpii map<int,int>
#define vi vector<int>
#define pii pair<int,int>
#define vpii vector<pair<int,int> >
#define rep(i, a, n) for (int i=a;i<n;++i)
#define per(i, a, n) for (int i=n-1;i>=a;--i)
#define clr clear
#define pb push_back
#define mp make_pair
#define fir first
#define sec second
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define lson l, mid, rt<<1
#define rson mid+1, r, rt<<1|1 typedef long long LL;
double inf = 1e18 + ;
double bound = 5e18 + ;
LL c[];
LL a, b; LL Pow(LL base, LL n) {
LL ret = ; while (n) {
if (n & ) {
if (inf/base < ret) return -;
ret = ret * base;
} n >>= ;
if (bound/base<base && n>) return -;
base = base * base;
} return ret;
} LL gao(LL v, LL n) {
LL x = pow((double)v, 1.0/n);
LL tmp = Pow(x, n);
if (tmp == v) return x;
if (tmp>v || tmp==-) {
--x;
} else {
tmp = Pow(x+, n);
if (tmp!=- && tmp<=v) ++x;
} return x;
} LL calc(LL n) {
int i; memset(c, , sizeof(c));
c[] = n;
for (i=; i<; ++i) {
c[i] = gao(n, i) - ;
if (c[i] == ) break;
} per(j, , i) {
rep(k, , j) {
if (j%k == )
c[k] -= c[j];
}
} LL ret = ;
rep(j, , i) ret += j*c[j];
return ret;
} void solve() {
LL ans = calc(b) - calc(a-);
printf("%I64d\n", ans);
} int main() {
cin.tie();
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif while (scanf("%I64d%I64d", &a,&b)!=EOF && (a||b)) {
solve();
} #ifndef ONLINE_JUDGE
printf("time = %ldms.\n", clock());
#endif return ;
}
【HDOJ】3208 Integer’s Power的更多相关文章
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- HDU 3208 Integer’s Power
Integer’s Power Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Origina ...
- hdu 3208 Integer’s Power 筛法
Integer’s Power Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 【HDOJ】Power Stations
DLX.针对每个城市,每个城市可充电的区间构成一个plan.每个决策由N*D个时间及N个精确覆盖构成. /* 3663 */ #include <iostream> #include &l ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】4426 Palindromic Substring
综合性很强的一道题目,结合manacher,后缀数组,哈希,RMQ,二分可解.基本思路是通过manacher可以找到所有可能的回文串,哈希去重,后缀数组二分找数目.最后暴力求解.需要注意kth需要为_ ...
- 【PAT】1103 Integer Factorization(30 分)
The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
随机推荐
- SPOJ - DQUERY 主席树
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=32356 Given a sequence of n numbers ...
- Basic knowledge of javaScript (keep for myself)
1. 函数表达式 JavaScript 函数可以通过一个表达式定义.eg. var x = function (a, b) {return a * b}; so: var x = function ( ...
- NYOJ-73 比大小 AC 分类: NYOJ 2014-01-17 21:29 195人阅读 评论(0) 收藏
典型的大数题目,这只是大数的比较,到时还有大数加减乘除,更加还有乘方,对于大数,一般用数组或者字符串,因为其他的结构类型一般都没有那么大 的范围!! 这道题目需要你仔细回想怎么比较俩个数字的大小,考虑 ...
- GS玩家登录
玩家上线 这个过程看了很多很多次了,这里在看下 客户端打开,服务器收到libevent事件,然后new Channel这个过程都付给他各种指针,然后放到channel容器中 .客户端发送c2s_log ...
- NF3 里面的z cull reverse reload
nf3 siggraph2011的 分享 里面有谈对csm的优化. 1.mask white red 2. HI Z 这俩我都懂 3.reverse depth buffer这实在不明白, 为什么会有 ...
- Sql注入一种dump所有数据的方法
Select exp(~(select*from(select(concat(@:=0,(select count(*)from`information_schema`.columns where t ...
- JavaScript js 精确、保留小数方法
//保留两位小数 //功能:将浮点数四舍五入,取小数点后2位 function toDecimal(x) { var f = parseFloat(x); if (isNaN(f)) { return ...
- leetcode majority number
给定一组数,有一个数在这组数里的出现次数超过n/2次. 求出这是哪个数 https://leetcode.com/problems/majority-element/ 一开始考虑的方是将所有数转化为二 ...
- 斌哥的 Docker 进阶指南—监控方案的实现
过去的一年中,关于 Docker 的话题从未断过,而如今,从尝试 Docker 到最终决定使用 Docker 的转化率依然在逐步升高,关于 Docker 的讨论更是有增无减.另一方面,大家的注意力也渐 ...
- 安装WINCC6.0的步骤
安装WINCC6.0/6.2的步骤 (XP不能是HOME版的!!!) 1. 首先安装SQL FOR WINCC6.0/6.2这个软件(如果你的系统已安装此软件相关版本可能提示安装失败请卸载后再重 ...