TOJ 4373 HDU 4430 ZOJ 3665 Yukari's Birthday / 二分

Yukari's Birthday

时间限制(普通/Java):1000MS/3000MS     运行内存限制:65536KByte

描述

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 placen 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 liker ≥ 1 concentric circles. They place ki candles equidistantly on thei-th circle, where k ≥ 2, 1 ≤ ir. 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 ofr and k satisfying these restrictions, they want to minimizer × k. If there is still a tie, minimize r.

输入

There are about 10,000 test cases. Process to the end of file.

Each test consists of only an integer 18 ≤ n ≤ 1012.

输出

For each test case, output r and k.

样例输入

18
111
1111

样例输出

1 17
2 10
3 10

假设k最小是2 那么r最大45 2的45次超过了10的12次

然后枚举r 二分k

二分时左值l明显是从2 r的话我从100w开始 因为100w的平方就大于等于n最大

对于二分的左右范围不是很会判断

烂代码

#include <stdio.h>
const long long  MAX = 1000010;
long long n;

long long check(long long k,long long r)
{
	long long res = 0,i,m = k;
	for(i = 1;i <= r; i++)
	{
		res += m;
		//printf("%I64d\n",res);
		m *= k;
		if(res > n)
			return MAX * MAX;
	}
	return res;
}
long long erfen(long long l,long long r,long long k)
{
	if(k == 1)
		return n - 1;
	while(l <= r)
	{
		long long m = (l + r) >> 1;
		long long res = check(m,k);
		//printf("%I64d\n",res);
		if(res == n || res == n - 1)
			return m;
		
		if(res > n)
			r = m - 1;
		else
			l = m + 1;
	}
	return -1;
}
int main()
{
	long long l,r,i,R,K,RR,KK;
	while(scanf("%lld",&n)!=EOF)
	{	
		RR = MAX;
		KK = MAX;
		for(i = 1;i <= 40; i++)
		{
			l = 2;
			r = 1000000;
			long long res = erfen(l,r,i);
			if(res != -1)
			{
				if(res * i < RR * KK || res * i == RR * KK && i < RR)
				{
					RR = i;
					KK = res;
				}
			}
		}
		printf("%lld %lld\n",RR,KK);
	}
	return 0;
}
	


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值