HDU - 4430 Yukari's Birthday(二分+枚举)
题意:已知有n个蜡烛,过生日在蛋糕上摆蜡烛,将蜡烛围成同心圆,每圈个数为ki,蛋糕中心最多可摆一个蜡烛,求圈数r和看,条件为r*k尽可能小的情况下,r尽可能小。
分析:n最大为1012,k最少为2,假设k为2,r最多为40,因此枚举r,二分k。
需要两个剪枝防止爆LL,
在计算ans=k1+k2+……+kr的过程中
(1)当kr>n时,break,并向左区间继续搜索
(2))当ans>n时,break,并向左区间继续搜索
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 10000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
LL n;
struct Node{
int r;
LL k;
bool operator < (const Node &rhs)const{
return r * k < rhs.r * rhs.k || (r * k == rhs.r * rhs.k && r < rhs.r);
}
}num[50];
LL deal(LL k, int r){
LL ans = 0;
LL tmp = 1;
bool ok = true;
for(int i = 1; i <= r; ++i){
if(n / tmp < k){
return -1;
}
tmp *= k;
ans += tmp;
if(ans > n){
return -1;
}
}
return ans;
}
LL solve(int r){
LL L = 2, R = n;
while(L <= R){
LL mid = L + (R - L) / 2;
LL tmp = deal(mid, r);
if(tmp == n || tmp == n - 1) return mid;
if(tmp == -1) R = mid - 1;
else if(tmp < n - 1) L = mid + 1;
}
return -1;
}
int main(){
while(scanf("%lld", &n) == 1){
int cnt = 0;
for(int r = 1; r <= 40; ++r){
LL k = solve(r);
if(k == -1) continue;
num[cnt].r = r;
num[cnt++].k = k;
}
sort(num, num + cnt);
printf("%d %lld\n", num[0].r, num[0].k);
}
return 0;
}
HDU - 4430 Yukari's Birthday(二分+枚举)的更多相关文章
- HDU 4430 Yukari's Birthday (二分+枚举)
题意:给定一个n(18 ≤ n ≤ 10^12),一个等比数列k + k^2 + .......+ k^r = n 或者 = n-1,求出最小的k*r,如果最小的不唯一,则取r更小的 分析:两个未知数 ...
- HDU 4430 Yukari's Birthday (二分)
题意:有 n 个蜡烛,让你插到蛋糕上,每一层要插 k^i个根,第0层可插可不插,插的层数是r,让 r * k 尽量小,再让 r 尽量小,求r 和 k. 析:首先先列出方程来,一个是不插的一个是插的,比 ...
- hdu 5248 序列变换(二分枚举)
Problem Description 给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+,≤i<N). 我们 ...
- hdu 4430 Yukari's Birthday 枚举+二分
注意会超long long 开i次根号方法,te=(ll)pow(n,1.0/i); Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others) ...
- hdu 4430 Yukari's Birthday (简单数学 + 二分)
Problem - 4430 题意是,给出蜡烛的数量,要求求出r和k,r是蜡烛的层数,k是每一层蜡烛数目的底数. 开始的时候,没有看清题目,其实中间的那根蜡烛是可放可不放的.假设放置中间的那根蜡烛,就 ...
- HDU 4430 Yukari's Birthday(二分)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4430 题目大意:给定n个蜡烛,围绕蛋糕的中心插同心圆,从里往外分别是第1圈.第2圈....第r圈,第 ...
- hdu 4430 Yukari's Birthday
思路: 分析知道1<=r<40:所以可以枚举r,之后再二分k. 代码如下: #include<iostream> #include<stdio.h> #includ ...
- hdu 4430 二分+枚举
/* 二分+枚举 枚举k会超时,枚举r还要优化,有可能会超64 */ #include<stdio.h> #include<math.h> #define ll __int64 ...
- HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...
随机推荐
- mabatis--动态sql
1.mybatis核心,对sql语句进行灵活操作,通过表达式进行判断,对sql进行灵活拼接.组装: 2.使用if判断: <where> <if test="customer ...
- 大数据篇:DolphinScheduler-1.2.0.release安装部署
大数据篇:DolphinScheduler-1.2.0.release安装部署 1 配置jdk #查看命令 rpm -qa | grep java #删除命令 rpm -e --nodeps xxx ...
- Day4-F-产生冠军 HDU - 2094
有一群人,打乒乓球比赛,两两捉对撕杀,每两个人之间最多打一场比赛. 球赛的规则如下: 如果A打败了B,B又打败了C,而A与C之间没有进行过比赛,那么就认定,A一定能打败C. 如果A打败了B,B又打败了 ...
- Zero 初识Sciter
在浏览有关Sciter技术前,您需要花点时间浏览以下内容. 您是否需要花时间学习Sciter? 如果您的工作或您想从事的工作与桌面应用开发无关,那么您不需要学习Sciter. 如果您不认同HTML\C ...
- 5.2 Nginx Http 反向代理
- FTP大量数据(百万级数据)传输尝试
接上篇<Java FTPClient 大量数据传输的问题(未解决)> 因为数据量过大 FTPClient获取文件列表信息会出现不稳定的问题,找了很多方法都没有解决. 网上找的这一篇文章 h ...
- [AHK]输入法状态提示,中文状态提示“中”,英文状态提示“EN”[转]
测试中发现使用小鹤双拼,如果不是使用shift切换中英文,效果不理想,留下此文章做个标记 https://www.autoahk.com/archives/8755 输入法状态提示,中文状态提示&qu ...
- SVG格式文件
SVG可以算是目前最最火热的图像文件格式了,它的英文全称为Scalable Vector Graphics,意思为可缩放的矢量图形.它是基于XML(Extensible Markup Language ...
- 白手起家Django项目发布下篇_Django项目nginx部署
上一篇完成了python的安装,接下来安装python的依赖包和项目的依赖包 1. python-devel 命令:yum -y install python-devel 安装Django1.8.2 ...
- P1060 爱丁顿数
P1060 爱丁顿数 转跳点: