题意:已知有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,并向左区间继续搜索

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cctype>
  5. #include<cmath>
  6. #include<iostream>
  7. #include<sstream>
  8. #include<iterator>
  9. #include<algorithm>
  10. #include<string>
  11. #include<vector>
  12. #include<set>
  13. #include<map>
  14. #include<stack>
  15. #include<deque>
  16. #include<queue>
  17. #include<list>
  18. #define lowbit(x) (x & (-x))
  19. const double eps = 1e-8;
  20. inline int dcmp(double a, double b){
  21. if(fabs(a - b) < eps) return 0;
  22. return a > b ? 1 : -1;
  23. }
  24. typedef long long LL;
  25. typedef unsigned long long ULL;
  26. const int INT_INF = 0x3f3f3f3f;
  27. const int INT_M_INF = 0x7f7f7f7f;
  28. const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
  29. const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
  30. const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
  31. const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
  32. const int MOD = 1e9 + 7;
  33. const double pi = acos(-1.0);
  34. const int MAXN = 10000 + 10;
  35. const int MAXT = 10000 + 10;
  36. using namespace std;
  37. LL n;
  38. struct Node{
  39. int r;
  40. LL k;
  41. bool operator < (const Node &rhs)const{
  42. return r * k < rhs.r * rhs.k || (r * k == rhs.r * rhs.k && r < rhs.r);
  43. }
  44. }num[50];
  45. LL deal(LL k, int r){
  46. LL ans = 0;
  47. LL tmp = 1;
  48. bool ok = true;
  49. for(int i = 1; i <= r; ++i){
  50. if(n / tmp < k){
  51. return -1;
  52. }
  53. tmp *= k;
  54. ans += tmp;
  55. if(ans > n){
  56. return -1;
  57. }
  58. }
  59. return ans;
  60. }
  61. LL solve(int r){
  62. LL L = 2, R = n;
  63. while(L <= R){
  64. LL mid = L + (R - L) / 2;
  65. LL tmp = deal(mid, r);
  66. if(tmp == n || tmp == n - 1) return mid;
  67. if(tmp == -1) R = mid - 1;
  68. else if(tmp < n - 1) L = mid + 1;
  69. }
  70. return -1;
  71. }
  72. int main(){
  73. while(scanf("%lld", &n) == 1){
  74. int cnt = 0;
  75. for(int r = 1; r <= 40; ++r){
  76. LL k = solve(r);
  77. if(k == -1) continue;
  78. num[cnt].r = r;
  79. num[cnt++].k = k;
  80. }
  81. sort(num, num + cnt);
  82. printf("%d %lld\n", num[0].r, num[0].k);
  83. }
  84. return 0;
  85. }

  

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. 「CF650E」Clockwork Bomb

    传送门 Luogu 解题思路 显然对于两棵树共有的边,我们不会动它. 考虑第二颗树中有和第一棵树不同的边怎么处理. 我们设 \(fa_1[u],fa_2[u]\) 分别代表 \(u\) 在两棵树中的父 ...

  2. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 图片

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 2017 青岛网络赛 Chenchen, Tangtang and ZengZeng

    Chenchen, Tangtang and ZengZeng are starting a game of tic-tac-toe, played on a 3 × 3 board. Initial ...

  4. JS开发常用工具函数

    1.isStatic:检测数据是不是除了symbol外的原始数据 function isStatic(value) { return ( typeof value === 'string' || ty ...

  5. 关于netty配置的理解serverBootstrap.option和serverBootstrap.childOption

    The parameters that we set using ServerBootStrap.option apply to the ChannelConfig of a newly create ...

  6. java面试题汇总,不断更新中。。。

    JVM,并发,锁相关: 1.请你谈谈对volatile的理解,volatile是否存在伪共享问题. 2.cas你知道吗? 3.原子类AtomicInteger的ABA问题谈谈?原子更新引用知道吗? 4 ...

  7. Saul's Blog

    2019Falg完成情况 - 脱贫脱单不脱发(已完成) - 买辆帕拉梅拉 (已完成) - 不再是个蒟蒻(已完成) - 来一场说走就走的旅行(已完成) - 停止口嗨(未完成) ᑋᵉᑊᑊᵒ ᵕ̈ ₂₀₂₀ ...

  8. Spring boot application.properties和 application.yml 初学者的学习

    来自于java尚硅谷教程 简单的说这两个配置文件更改配置都可以更改默认设置的值比如服务器端口号之类的,只需再文件中设置即可, properties可能是出现的比较早了,如果你不调你的默认编码,中文可能 ...

  9. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-pencil

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...

  10. 剑指offer自学系列(四)

    题目描述: 输入一个正整数数组,把数组里面所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个,例如输入数组{3,32,321},输出的最小数字为321323 题目分析: 如果采用穷举法,把 ...