Yukari's Birthday 枚举+二分 过程注意数据的溢出问题 HDU4430

Yukari's Birthday

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1993    Accepted Submission(s): 404


Problem Description
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 ≤ i ≤ r. 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
 
Source
 
Recommend
zhuyuanchen520   |   We have carefully selected several similar problems for you:  4769 4768 4767 4766 4765 
 
/*
 * Author:  
 * Created Time:  2013/10/25 20:10:41
 * File Name: A.cpp
 * solve: A.cpp
 */
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
#include<set>
#include<iostream>
#include<vector>
#include<queue>
//ios_base::sync_with_stdio(false);
//#pragma comment(linker, "/STACK:1024000000,1024000000")

using namespace std;
#define sz(v) ((int)(v).size())
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define repf(i, a, b) for (int i = (a); i <= (b); ++i)
#define repd(i, a, b) for (int i = (a); i >= (b); --i)
#define clr(x) memset(x,0,sizeof(x))
#define clrs( x , y ) memset(x,y,sizeof(x))
#define out(x) printf(#x" %d
", x)
#define sqr(x) ((x) * (x))
typedef long long LL;

const LL INF = 100000000000000000;
const double eps = 1e-8;
const int maxn = 30000;

int sgn(const double &x) {  return (x > eps) - (x < -eps); }

LL Pow(LL a,int b)
{
    LL ans = 1;
    repf(i,1,b)
        ans*= a;
    return ans;
}
int main() 
{
    //freopen("in.txt","r",stdin);
    LL n;
    while(scanf("%I64d",&n) == 1)
    {
        LL k,r;
        LL Max = INF;
        LL ans1 = INF;
        LL ans2 = INF;
        repf(i,2,50)
        {
            r = i;
            LL L = 2;
            LL R = pow(n,1.0/i);
            k = -1;
            while(L <= R)
            {
                LL mid = (L + R)/2;
                LL temp = Pow(mid,i+1);
                if((temp - 1)%(mid - 1) == 0)
                {
                    if((temp-1)/(mid-1) == n || (temp-1)/(mid-1) == n+1)
                    {
                        k = mid;
                        break;
                    }
                }
                if((temp-1)/(mid-1) <= n)
                    L = mid + 1;
                if((temp-1)/(mid-1) >= n+1)
                    R = mid - 1;
            }
            
            if(k == -1)
            {
                continue;
            }
            
            if(k*r < Max)
            {
                ans1 = r;
                ans2 = k;
                Max = k*r; 
            }
            if(Max == k*r)
            {
                ans1 = min(ans1,r);
            }
        }
        
        if(ans1 == INF)
        {
            ans1 =  1;
            ans2 = n - 1;
        }
        cout<<ans1<<" "<<ans2<<endl;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3388568.html