【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 总结:处理整数溢出 ...
随机推荐
- Leetcode#135 Candy
原题地址 遍历所有小孩的分数 1. 若小孩的分数递增,分给小孩的糖果依次+12. 若小孩的分数递减,分给小孩的糖果依次-13. 若小孩的分数相等,分给小孩的糖果设为1 当递减序列结束时,如果少分了糖果 ...
- Poj 2349 Arctic Network 分类: Brush Mode 2014-07-20 09:31 93人阅读 评论(0) 收藏
Arctic Network Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9557 Accepted: 3187 De ...
- [Js/Jquery]天气接口简单使用
写在前面 今天在群里有朋友使用一个天气api,觉得挺实用的,就记录一下.省的以后再花费功夫去找. 地址:http://www.k780.com/api,在这个网站提供了实用的几种接口,比如查询ip,天 ...
- hbase表结构设计
非常好的一个ppt HBase Schema design: http://www.slideshare.net/cloudera/5-h-base-schemahbasecon2012
- React Native 简介:用 JavaScript 搭建 iOS 应用(2)
[编者按]本篇文章的作者是 Joyce Echessa--渥合数位服务创办人,毕业于台湾大学,近年来专注于协助客户进行 App 软体以及网站开发.本篇文章中,作者介绍通过 React Native 框 ...
- java基础知识回顾之接口
/* abstract class AbsDemo { abstract void show1(); abstract void show2(); } 当一个抽象类中的方法都是抽象的时候,这时可以将该 ...
- LINUX进程控制
1. 引言 一个程序是存储在文件中的机器指令序列.一般它是由编译器将源代码编译成二进制格式的代码.运行一个程序意味着将这个机器指令序列载入内存然后让处理器(cpu)逐条执行这些指令. 在Unix术语中 ...
- JavaScript基础(一)
我是一个初学者,但求能学到些许知识!以下是根据韩顺平老师的<轻松搞定网页设计html+css+javascript—javascrip部分>整理而成. 为什么要学习javascript? ...
- Android NDK引用预编译的动态链接库
NDK里有个例子: android-ndk-r10/samples/module-exports/jni一看就懂了 ———————————————————————————– 从r5版本开始,就支持预编 ...
- ANDROID STUDIO, GRADLE AND NDK INTEGRATION
Originally posted on:http://ph0b.com/android-studio-gradle-and-ndk-integration/ With the recent chan ...