zoj 3665 Yukari's Birthday【二分】

题意:给出一个数n,18<=n<=10^12,代表有n个蜡烛,要把这n个蜡烛插到蛋糕上,0层最多可以插一个,一共r层,第一层k个,第二层k^2个,……,第r层k^r个,

输出r和k,情况多种输出r*k最小的,r*k最小的有多种输出r最小的。

代码:

#include<stdio.h>
#include<math.h>

struct node {
	long long r;
	long long k;
	long long tot;
};

int main()
{
	long long n;
	while(scanf("%lld", &n) != EOF) {
		struct node data;

		data.r = 1;
		data.k = n-1;
		data.tot = n-1;
		for(int r = 2; r <= 40; r++) {
			long long high = (long long)pow(n, 1.0/r);
			long long low = 2, mid;
			bool bl = 0;

			while(low <= high) {
				mid = (low+high)/2;
				long long ans;

				ans = mid*(1-(long long)pow(mid, r))/(1-mid);
				if(ans == n || ans == n-1) {
					bl = 1;
					break;
				} else if(ans > n) {
					high = mid - 1;
				} else {
					low = mid + 1;
				}
			}

			if(data.tot == mid * r && bl) {
				data.r = data.r < r ? data.r : r;
				data.k = data.r < r ? data.k : mid;
				continue;
			}
			if(bl) {
				data.tot = data.tot < mid * r ? data.tot : mid * r;
				data.r = data.tot < mid * r ? data.r : r;
				data.k = data.tot < mid * r ? data.k : mid;
			}
		}
		printf("%lld %lld\n", data.r, data.k);
	}
	return 0;
}

  

posted @ 2012-10-16 00:07  小猴子、  阅读(887)  评论(7编辑  收藏  举报