hdu4430 Yukari's Birthday 枚举+二分

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.


嗯,题意就是紫妈要过生日了,所以蓝和橙要给她买蛋糕,然后插对应年龄的蜡烛(明明应该只插17个的,式神们都不要命了。。。)

然后蜡烛插 r 圈 每圈插k^n个蜡烛,中心可以插一只蜡烛或者不插,问最后r*k最小的插法是什么,如果有相同就输出r最小的。

2的39次方就是5e12了,从2扫到40即可

枚举r,对k进行二分找到满足条件的k值,注意一下r的初值不能太大,否则会wa,原因不明

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<vector>
#include<string>
#include<cmath>
#include<stack>
using namespace std;
const long long inf=1e16;
typedef long long ll;
ll n;
int calc(ll mid,int num)
{
	ll ansav=mid;
	ll ans=0;
	for(int i=1;i<=num;i++)
	{
		ans+=ansav;
		//if(ansav==17) printf("ans=%lld\n",ans);
		ansav*=mid;
	}
	if(ans<0) return 1;
	else if(ans==n||ans+1LL==n) return 2;
	else if(ans < n) return 0;
	else return 1;

}
//long long ans=(mid-powLL(mid,i+1))/(1-mid);
int main()
{
    //printf("%lld\n",1LL<<39);
    while(scanf("%lld",&n)!=EOF)
	{
		ll savr=1,savk=n-1;
		ll ans=n-1;
		for(int i=2;i<=40;i++)
		{
			ll l=2,r=(long long)pow(n,1.0/i);
			while(l<=r)
			{
				ll mid=(l+r)/2;
				//if(i==5) printf("%lld %lldmid=%lld\n",l,r,mid);
				if(calc(mid,i)==2)
				{
					if(ans>mid*i)
					{
						ans=mid*i;
						savr=i;
						savk=mid;
						//printf("%lld %lld %lld\n",savr,savk,ans);
					}
					break;
				}
				else if(calc(mid,i)==0) l=mid+1;
				else r=mid-1;
			}
		}
                //printf("%d\n",17); 紫妹永远17岁
		printf("%I64d %I64d\n",savr,savk);
	}
    return 0;
}


//紫大人我喜欢你啊

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值