HDOJ 4430 Yukari's Birthday 【枚举】+【二分】

题意:有一个蛋糕,将所有的蜡烛摆成一个以中心为同心轴的同心圆(中心可以放一个或者一个也不放,由近到远编号(1~r)每一个圆上分别放k^i(i是第几个的序号, k》=2)), 给你总的蜡烛数,让你求出k*r最小的,如果k*r相等,取r较小的。

分析:由等比数列可得k^0+k^1+。。。+k^r = (1-k^(r+1))/(1-k) 小于等于10e12,k最小是2,算出来r《40,那么我们可以枚举r,然后二分查找k,但是如果按照正常的二分,TL了(6s都TL。。。),分析发现,算幂的时候可能会溢出(64位也会溢出),当我们算到k^i <
n如果溢出的话肯定有k^i>n   =>k^(i-1)*k>n  => n/k^(i-1) < k  这样就可以防止溢出了。

而且,计算得每一个对于每一个r要想k^r<=n  k最大等于pow(n, 1.0/r)(仔细想一下)。

代码(200+):

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define LL __int64
#define M 100000
struct node{
	LL r, k, j;
}s[M];
LL tot, n;
bool cmp(node a, node b){
	if(a.j == b.j) return a.r<b.r;
	return a.j<b.j;
}
void solve(){
	LL i, left, right, mid, sum;
	for(i = 1; i < 45; i ++){
		left = 2; right = pow(n, 1.0/i); //这里可以换成n,我的运行时间是800+
		while(left <= right){
			sum = 0;
			LL temp = 1;
			mid = (left+right)>>1;
			for(LL j = 1; j <= i; j ++){
				if(n/temp < mid){ //
					sum = n+1;
					break;
				}
				temp *= mid;
				sum += temp;
			}
			if(sum == n||sum == n-1){
				s[tot].k = mid;
				s[tot].r = i;
				s[tot++].j = mid + i;
				break;
			}
			else if(sum < 0||sum> n) right = mid-1;
			else left = mid+1;
		}
	}
}
int main(){
	while(~scanf("%I64d", &n)){
		tot = 0;
		solve();
		sort(s, s+tot, cmp);
		printf("%I64d %I64d\n", s[0].r, s[0].k);
	}
	return 0;
} 

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4430

HDOJ 4430 Yukari's Birthday 【枚举】+【二分】

时间: 2024-10-21 21:16:17

HDOJ 4430 Yukari's Birthday 【枚举】+【二分】的相关文章

hdu 4430 Yukari&#39;s Birthday 枚举+二分

注意会超long long 开i次根号方法,te=(ll)pow(n,1.0/i); Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3262    Accepted Submission(s): 695 Problem Description Today is Yukari's n-th birt

hdu 4430 Yukari&#39;s Birthday(二分)

Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2703    Accepted Submission(s): 556 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party

HDOJ 4430 Yukari&#39;s Birthday

C++高精度问题太蛋疼了.... Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2891    Accepted Submission(s): 604 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a

HDU - 4430 - Yukari&#39;s Birthday

题目链接:https://vjudge.net/problem/HDU-4430 题目大意: 给出一个n,求出一个r和k,使得首项是k,公比是k的前0~r项和为n,且r*k尽量小. 题目分析: 首先利用等比数列的前n项和公式大概估算出r的大小范围大概最多为45左右. 此时可以枚举r,二分k,求得符合题意的r和k. 注意在二分的过程中可能会溢出,要进行预处理. 注:一开始n开成了int ,WA了一晚上,有毒. 给出代码: #include <cstdio> #include <iostre

hdu4430之枚举+二分

Yukari's Birthday Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2549    Accepted Submission(s): 522 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party

hdoj 4004 The Frog&#39;s Games(二分)

The Frog's Games Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)Total Submission(s): 5676    Accepted Submission(s): 2732 Problem Description The annual Games in frogs' kingdom started again. The most famous game i

CSU OJ PID=1514: Packs 超大背包问题,折半枚举+二分查找。

1514: Packs Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 61  Solved: 4[Submit][Status][Web Board] Description Give you n packs, each of it has a value v and a weight w. Now you should find some packs, and the total of these value is max, total of

Eqs 折半枚举+二分查找 大水题

Eqs 题目抽象:a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 (*),给出a1,a2,a3,a4,a5.    ai属于[-50,50]. 求有多少序列   x1,x2,x3,x4,x5 ,xi属于 [-50,50]-{0}. 思路:折半枚举+二分查找 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #inclu

UVA10277 - Boastin&#39; Red Socks(枚举+二分)

UVA10277 - Boastin' Red Socks(枚举+二分) 题目链接 题目大意:现在有m只红袜子,n只黑袜子,这样总袜子total = n + m;现在给你p, q,确定n和m,使得从这些袜子中取两只都是红袜子的概率等于p/q:如果没有这样的n和m满足要求输出impossible; 解题思路:m *(m - 1) / (total * (total - 1)) = p /q; 那么我们只需要枚举total,就可以解到m.但是会有精度误差,貌似有解决的办法,但是觉得没法理解.后面看了