【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 总结:处理整数溢出 ...
随机推荐
- hdu 3549 Flow Problem 网络流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Network flow is a well-known difficult problem f ...
- 【BZOJ】【1597】【USACO 2008 Mar】土地购买
DP/斜率优化 Orz Hzwer…… 想到排序了,但没想到其实可以将序列转化为x递增且y递减的序列……因为x是递增的,若y[i]>y[i-1]那么第i-1个就足够小……以至于可以在搞定第 i ...
- 通过HTTP访问网络资源
添加访问网络的权限:<uses-permission android:name="android.permission.INTERNET"/> package com. ...
- 创建第一个MVC
创建第一个MVC(asp.net)和默认路由设置 Asp.net的MVC已经出到了4.0,我用的是visual studio2013,接下来努力学下MVC,学之前的话我建议大家先去学下三层(分别是DA ...
- html之cellspacing && cellpadding讲解
单元格间距(表格间距)(cellspacing) -- 代表表格边框与单元格补白的距离,也是单元格补白之间的距离 单元格边距(表格填充)(cellpadding) -- 代表单元格外面的一个距离,用于 ...
- topcoder 642
A:直接拆开字符串写就好了. 今天的题目比较容易一些: B:题目大意: 求最少的翻转次数,每次翻转i是对应 y%i==0都会翻转. 球所有翻转为off状态的最小次数: 从最小idx开始,依次做就好了, ...
- 关于SOAP
http://www.tutorialspoint.com/soap/index.htm http://www.w3.org/TR/2000/NOTE-SOAP-20000508/ SOAP协议规范介 ...
- js函数延迟执行
function delay(value){ //全局变量保存当前值 window._myTempDalayValue = value; setTimeout(function(){ //延时之后与全 ...
- √新技能Get - 教你发空白朋友圈
今天下午都被空白朋友圈刷屏了.空白朋友圈也即是在朋友圈里面发空消息,没有图片也没有文字,朋友圈动态是空空的.这是谁在恶搞呢?怎么实现呢? 怎么发空消息啊?其实这是为了帮助大家识别身边用iOS的小伙伴的 ...
- 一套名企WEB前端面试题,不提供答案
1.说说你对Doctype的理解 2.web产品开发的流程 3.说说你对盒子模型的理解 4.前端页面有哪三层构成,分别是什么?作用是什么? 5.行内元素有哪些?块级元素有哪些?他们如何相互转化? 6. ...