hdu 4430 Yukari's Birthday

题目地址:

http://acm.hdu.edu.cn/showproblem.php?pid=4430

题目:

Yukari's Birthday

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 227    Accepted Submission(s): 35


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 k i 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 ≤ 10 12.
 

Output
For each test case, output r and k.
 

Sample Input
  
  
18 111 1111
 

Sample Output
  
  
1 17 2 10 3 10


题意:

将n支生日蛋糕的蜡烛像同心圆那样围成一圈一圈的,最里面的那个圈只能是1支蜡烛,其他圈按k^i(i=1~r)的数量增长,问给定n支,求实r*k最小的r,k序列,如果有多个r*k值相同的最小值,进一步求r最小的序列。


题解:

抽象成数学问题就是:有符合以下条件的式子:k^0 + k^1 + k^2 + k^3 +......+k^r=n,求 对任意的r,k使r*k最小。注意下取值范围,k是>=2的 n<=10^12,那么很容易能估算出r最大值在40左右。时间要求也很充足,可以一次穷举r和k 以此求出最小值,这里r顺序枚举,在确定k值时用二分枚举,直接用公式求k不是很好求,并且要考虑浮点数误差的问题,这点很容易导致无限的WA中。


代码:

/*
hdu :Yukari's Birthday
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
using namespace std;
__int64 R=0,K=0,Value=0;//R 's max is about 40
__int64 MinR=0,MinK=0,MinValue=0;
__int64 n=0;
/*init the var*/
int InitVar()
{
	MinR=1;
	MinK=n-1;
	MinValue=n-1;
	return(0);
}
/*for test*/
int test()
{
	return(0);
}
/*main process*/
int MainProc()
{
	while(scanf("%I64d",&n)!=EOF)
	{
		InitVar();
		for(R=2;R<=40;R++)
		{
			__int64 KLow=2;
			__int64 KHigh=(__int64)pow((double)n,1.0/(double)R);
			while(KLow<=KHigh)
			{
				__int64 KMid=(KLow+KHigh)/2;
				//__int64 N=(__int64)((pow((double)KMid,(double)(R+1))-1)/(double)(KMid-1)+0.0001);
				__int64 N=0;
				int i=0;
				__int64 RR=1;
				for(i=0;i<R;i++)
				{
					RR*=KMid;
					N+=RR;
				}
				if(N==n||N==n-1)
				{
					K=KMid;
					Value=K*R;
					break;
				}
				else if(N>n)//K is too big
				{
					KHigh=KMid-1;
				}
				else
				{
					KLow=KMid+1;
				}
			}
			if(Value<=MinValue&&KLow<=KHigh)
			{
				if(Value<MinValue)
				{
					MinR=R;
					MinK=K;
					MinValue=Value;
				}
				else
				{
					if(R<MinR)
					{
						MinR=R;
						MinK=K;
						MinValue=Value;
					}
				}
			}
		}
		printf("%I64d %I64d\n",MinR,MinK);
	}
	return(0);
}
int main()
{
	MainProc();
	return(0);
}







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值