主题链接:

HDU:

pid=4430" target="_blank">http://acm.hdu.edu.cn/showproblem.php?pid=4430

ZJU:

problemId=4888" target="_blank">http://acm.zju.edu.cn/onlinejudge/showProblem.do?

problemId=4888

Problem Description
Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most important part, birthday cake! But it's a big challenge for them to place n candles on the top of the cake. As Yukari has lived for such a long long time, though
she herself insists that she is a 17-year-old girl.

To make the birthday cake look more beautiful, Ran and Chen decide to place them like r ≥ 1 concentric circles. They place ki candles equidistantly on the i-th circle, where k ≥ 2, 1 ≤ i ≤ r. And it's optional to place at most one candle at the center
of the cake. In case that there are a lot of different pairs of r and k satisfying these restrictions, they want to minimize r × k. If there is still a tie, minimize r.
 
Input
There are about 10,000 test cases. Process to the end of file.

Each test consists of only an integer 18 ≤ n ≤ 1012.
 
Output
For each test case, output r and k.
 
Sample Input
18
111
1111
 
Sample Output
1 17
2 10
3 10
 
Source

题意:

要在一个蛋糕上放置 n 根蜡烛,摆成 r 个同心圆,每一个同心圆的蜡烛数为 k ^ i ,中间的圆心能够放一根或者不放,使得 r * k 最小,若有多个答案输出 r 最小的那个。

PS:

由于r是非常小的 !

枚举r查找k。

代码例如以下:(HDU,ZOJ上把64位换为long long就OK啦……)

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
//typedef long long LL;
typedef __int64 LL;
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
LL n;
LL findd(LL m)
{
LL l, r, mid;
l = 2;
r = n;
while(l <= r)
{
mid = (l+r)/2;
LL sum = 0, tt = 1;
for(LL i = 1; i <= m; i++)
{
if(n/tt < mid)//注意可能溢出用除法推断一下
{
//tt*mid > n
sum = n+1;
break;
}
tt*=mid;
sum += tt;
// if(sum > n)//防止溢出
// break;
}
if(sum == n-1 || sum == n)
{
return mid;
}
if(sum < n-1)
{
l = mid+1;
}
else if(sum > n)
{
r = mid-1;
}
}
return -1;//没有符合的
} int main()
{
LL r, k, rr, kk;
while(~scanf("%I64d",&n))
{
rr = r = 1;
kk = k = n-1;
for(LL i = 2; i <= 64; i++)
{
LL tt = findd(i);
// if(i >= n)
// break;
// printf("tt:%I64d>>>%I64d\n",i,tt);
if(i*tt < rr*kk && tt != -1)
{
r = i;
k = tt;
rr = i;
kk = tt;
}
}
printf("%I64d %I64d\n",r,k);
}
return 0;
} /*
18
111
1111
1022
8190
134217726
34359738366
68719476734
*/

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分法+枚举)的更多相关文章

  1. zoj 3665 Yukari's Birthday(枚举+二分)

    Yukari's Birthday Time Limit: 2 Seconds       Memory Limit: 32768 KB Today is Yukari's n-th birthday ...

  2. HDU 4791 &amp; ZOJ 3726 Alice&#39;s Print Service (数学 打表)

    题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showP ...

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

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

  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

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

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

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

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

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

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

    题意:已知有n个蜡烛,过生日在蛋糕上摆蜡烛,将蜡烛围成同心圆,每圈个数为ki,蛋糕中心最多可摆一个蜡烛,求圈数r和看,条件为r*k尽可能小的情况下,r尽可能小. 分析:n最大为1012,k最少为2,假 ...

随机推荐

  1. 用java代码实现环圈报数

    环圈报数就是围一圈人,每一次数数数到三的人自动出圈,再接着数, 用数据结构的思想实现 public class Count3Quit {     public static void main(Str ...

  2. 【转】Acm之java速成

    这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入:格式为:Scanner cin = new Sca ...

  3. SlidingMenu开源项目滑动界面的实现总结

    先上图 须要准备的是先得在GitHub上下载ActionBarSherlock-master.zip,和SlidingMenu-master.zip这两个开源文件,然后解压这两个包,SlidingMe ...

  4. 三星galaxy S4快捷功能

    你不知道的s4那些快捷操作全面挖掘 1.截屏:S4有三种截屏方法: 一种是常见的同一时候按住home键和电源键大概2秒左右时间. 另外一种是打开手势感应,设定→我的设备→动作与手势→手掌动作→截取屏幕 ...

  5. Android至ViewPager添加切换动画——使用属性动画

    转载请注明出处:http://blog.csdn.net/allen315410/article/details/44200623 ViewPager作为Android最经常使用的的组件之中的一个.相 ...

  6. 基于.net开发chrome核心浏览器【四】

    原文:基于.net开发chrome核心浏览器[四] 一: 上周去北京出差,给国家电网的项目做架构方案,每天都很晚睡,客户那边的副总也这样拼命工作. 累的不行了,直接导致第四篇文章没有按时发出来. 希望 ...

  7. ECSHOP分类页面筛选功能(按分类下子分类和品牌筛选)

    其实分类页面里面本来就有相关的品牌.属性.分类的筛选功能在category.php和模板加上相应的功能即可 1.读出当前分类的所有下级分类 $chlidren_category = $GLOBALS[ ...

  8. qt的资源替换搜索QDir具体解释

    QDir对跨平台的文件夹操作提供了非常多的便利,为了更加方便的提供全局资源的查找,QDir提供了搜索路径替换功能,攻克了资源搜索不便的问题,也能提高文件查找的效率. QDir通过已知的路径前缀去搜索并 ...

  9. Java EE (9) -- JDBC & JTA

    Connection接口中定义了5中隔离级别常量 Connection.TRANSACTION_NONE  --  不支持事务 Connection.TRANSACTION_READ_UNCOMMIT ...

  10. mongodb实现简单的增删改查

    package mongoDB; import java.net.UnknownHostException; import java.util.ArrayList; import java.util. ...