题意:已知有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(二分+枚举)的更多相关文章

  1. HDU 4430 Yukari's Birthday (二分+枚举)

    题意:给定一个n(18 ≤ n ≤ 10^12),一个等比数列k + k^2 + .......+ k^r = n 或者 = n-1,求出最小的k*r,如果最小的不唯一,则取r更小的 分析:两个未知数 ...

  2. HDU 4430 Yukari's Birthday (二分)

    题意:有 n 个蜡烛,让你插到蛋糕上,每一层要插 k^i个根,第0层可插可不插,插的层数是r,让 r * k 尽量小,再让 r 尽量小,求r 和 k. 析:首先先列出方程来,一个是不插的一个是插的,比 ...

  3. hdu 5248 序列变换(二分枚举)

    Problem Description 给定序列A={A1,A2,...,An}, 要求改变序列A中的某些元素,形成一个严格单调的序列B(严格单调的定义为:Bi<Bi+,≤i<N). 我们 ...

  4. 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) ...

  5. hdu 4430 Yukari's Birthday (简单数学 + 二分)

    Problem - 4430 题意是,给出蜡烛的数量,要求求出r和k,r是蜡烛的层数,k是每一层蜡烛数目的底数. 开始的时候,没有看清题目,其实中间的那根蜡烛是可放可不放的.假设放置中间的那根蜡烛,就 ...

  6. HDU 4430 Yukari's Birthday(二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4430 题目大意:给定n个蜡烛,围绕蛋糕的中心插同心圆,从里往外分别是第1圈.第2圈....第r圈,第 ...

  7. hdu 4430 Yukari's Birthday

    思路: 分析知道1<=r<40:所以可以枚举r,之后再二分k. 代码如下: #include<iostream> #include<stdio.h> #includ ...

  8. hdu 4430 二分+枚举

    /* 二分+枚举 枚举k会超时,枚举r还要优化,有可能会超64 */ #include<stdio.h> #include<math.h> #define ll __int64 ...

  9. HDU 1669 Jamie's Contact Groups(多重匹配+二分枚举)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1669 题目大意: 给你各个人可以属于的组,把这些人分组,使这些组中人数最多的组人数最少,并输出这个人数 ...

随机推荐

  1. formatTime() 时间戳,返回数据是计算距离现在的时间

    const formatTime=function(tiem) {//时间转换   const timestamp = Date.now();   return function (tiem) {   ...

  2. Jquery动画效果设置大全

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. 使用Vue 和 内网穿透:返回 invalid host header

    原因:新版的webpack-dev-server出于安全考虑,默认检查hostname,如果它不是配置内的,将会中断访问. -------------------------------------- ...

  4. sessionManager配置

    在sessionManager配置的时候,有两个属性, 在这个类中,cacheManager是要注入到sessionDao中的,但要求sessionDao实现CacheManagerAware接口 C ...

  5. 51nod 1444:破坏道路 广度优先搜索

    1444 破坏道路 题目来源: CodeForces 基准时间限制:1.5 秒 空间限制:131072 KB 分值: 80 难度:5级算法题  收藏  取消关注 在某一个国家,那儿有n个城市,他们通过 ...

  6. VUE - 引入 npm 安装的模块 以及 uuid模块的使用

    <template>   <div>       <form @submit.prevent="addTodo">         <in ...

  7. js加密(十二)yy.com rsa

    1. url: https://aq.yy.com/ 2. target: 登录js 3. 是一个简单的rsa加密,找到加密的js文件,全部复制出来,修改一下就好. 4. 和网页中的一样

  8. [PHP] php作为websocket的客户端实时读取推送日志文件

    首先要使用composer来下载一个第三方扩展就可以实现php的websocket客户端,直接在当前目录生成下composer.json文件就可以了composer require textalk/w ...

  9. 044、Java中逻辑运算之向左边移位2位实现功能

    01.代码如下: package TIANPAN; /** * 此处为文档注释 * * @author 田攀 微信382477247 */ public class TestDemo { public ...

  10. 数据库建模工具pd的使用