HDU 4430

来源:互联网 发布:手机图纸算量软件 编辑:程序博客网 时间:2024/04/28 18:05

HDU 4430

Description

Today isYukari's n-th birthday. Ran and Chen hold a celebration party for her. Nowcomes the most important part, birthday cake! But it's a big challenge for themto place n candles on the top of the cake. As Yukari has lived for such a longlong 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 placethem like r ≥ 1 concentric circles. They place k i candlesequidistantly on the i-th circle, where k ≥ 2, 1 ≤ i ≤ r. And it's optional toplace at most one candle at the center of the cake. In case that there are alot of different pairs of r and k satisfying these restrictions, they want tominimize r × k. If there is still a tie, minimize r. 

Input

There are about10,000 test cases. Process to the end of file. 
Each test consists of only an integer 18 ≤ n ≤ 10 12

Output

For each testcase, output r and k.

Sample Input

18

111

1111

Sample Output

1 17

2 10

3 10

 

一.题意分析

蛋糕从内到外是一组同心圆,有r层,最中间放一个蜡烛,然后往外每一层放ki个蜡烛。要求r*k最小,并且输出最小时的r。

二.思路过程

先估算出r的大小范围,大概在40左右。然后对r的范围进行二分,最后枚举出结果。

三.代码

<span style="font-family:Courier New;font-size:18px;">#include <iostream>#include <cstdio>using namespace std;#define ll long long ll n; ll erfen(ll t){    ll i;    ll left = 2, right = n, mid;    while(left <= right)    {        ll sum = 1, ans = 0;        mid = (left + right) / 2;        for(i=1; i<=t; i++)        {            if(n/sum<mid)            {                ans = n + 1;                break;            }             sum*=mid;            ans+=sum;            if(ans>n)                break;        }         if(ans == n || ans == n-1)            return mid;        else if(ans < n-1)            left = mid + 1;        else            right = mid -1;    }    return -1;} int main(){    ll i, left, right, mid, tmp;    while(scanf("%lld", &n)!=EOF)    {        left = 1, right = n - 1;        for(i=2; i<45; i++)        {            tmp = erfen(i);            if(tmp!=-1 && i * tmp < left * right)            {                left = i, right = tmp;            }        }        printf("%lld %lld\n", left, right);    }    return 0;}</span>


0 0
原创粉丝点击