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
#include
#include
#include
#include
#include
#include
#include
#include
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;
}


//紫大人我喜欢你啊

你可能感兴趣的:(二分)