ZOJ 3665 Yukari's Birthday 第37届ACM/ICPC长春赛区现场赛K题 (水题,枚举,二分)

Yukari's Birthday

Time Limit: 2 Seconds      Memory Limit: 32768 KB

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 ki candles equidistantly on the i-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 of r and k satisfying these restrictions, they want to minimize r × k. If there is still a tie, minimize r.

Input

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

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

Output

For each test case, output r and k.

Sample Input

18
111
1111

Sample Output

1 17
2 10
3 10

Author: WU, Zejun
Contest: The 2012 ACM-ICPC Asia Changchun Regional Contest

 

 

长春现场赛时,第一道题就是做的这题。

就是枚举r,然后用二分来求对应的k.

由于题目上k>=2,所以r肯定不会很大,二分就可以了。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std;
long long solve(int r,long long sum)
{
    long long left=2,right=(long long)sqrt((double)sum);
    while(left<=right)
    {
        long long mid=(left+right)/2;
        double mm=(pow(1.0*mid,r+1)-mid)/(mid-1);
        if(mm>((double)sum+1e-2))
        {
            right=mid-1;
            continue;
        }
        long long temp=0;
        long long tt=1;
        for(int i=0;i<r;i++)
        {
            tt*=mid;
            temp+=tt;
        }
        if(temp==sum)return mid;
        else if(temp<sum)left=mid+1;
        else right=mid-1;
    }
    return 0;
}

int main()
{
    long long n;
    long long ans,ansk;
    int ansr;
    while(scanf("%lld",&n)!=EOF)
    {
        ans=n-1;
        ansr=1;
        ansk=n-1;
        for(int r=2;r<=40;r++)
        {
            long long temp=solve(r,n);
            //printf("%d\n",temp);
            if(temp==0)continue;
            if(r*temp<ans)
            {
                ans=r*temp;
                ansr=r;
                ansk=temp;
            }
            else if(r*temp==ans&&r<ansr)
            {
                ansr=r;
                ansk=temp;
            }
        }
        for(int r=2;r<=40;r++)
        {
            long long temp=solve(r,n-1);
            if(temp==0)continue;
            if(r*temp<ans)
            {
                ans=r*temp;
                ansr=r;
                ansk=temp;
            }
            else if(r*temp==ans&&r<ansr)
            {
                ansr=r;
                ansk=temp;
            }
        }
        printf("%d %lld\n",ansr,ansk);
    }
    return 0;
}

 

posted on 2012-10-16 12:29  kuangbin  阅读(1600)  评论(7编辑  收藏  举报

导航

JAVASCRIPT: