HDOJ 4430 Yukari'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 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

2012 Asia ChangChun Regional Contest

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

typedef long long int LL;

LL n;

LL check(LL r)
{
    LL low=2,high=n,mid,ret=-1;

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

        if(((LL)(pow(mid*1.,r-1.))>n||(LL)(pow(mid*1.,r-1.))<0)
           ||(LL)(pow(mid*1.,r*1.))>n||(LL)(pow(mid*1.,r*1.))<0)
        {
            high=mid-1;
            continue;
        }

        LL temp=mid*(1-(LL)(pow(mid*1.,r*1.)))/(1-mid);

        if(temp<=n-1||temp<=n)
        {
            if(temp==n||temp==n-1) ret=mid;
            low=mid+1;
        }
        else
        {
            high=mid-1;
        }
    }

    return ret;
}

int main()
{
while(scanf("%I64d",&n)!=EOF)
{
    LL R=1,K=n-1,ans=(1LL<<60);
    for(LL r=2;r<40;r++)
    {
        LL k=check(r);
        if(k==-1) continue;
        if(ans>r*k)
        {
            R=r; K=k;
        }
    }
    printf("%I64d %I64d\n",R,K);
}
    return 0;
}

HDOJ 4430 Yukari's Birthday

时间: 2024-12-17 14:57:01

HDOJ 4430 Yukari's Birthday的相关文章

HDOJ 4430 Yukari&#39;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...),分析发现,算幂的时候可能会溢

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

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

HDU 4430 &amp; ZOJ 3665 Yukari&#39;s Birthday(二分+枚举)

题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4430 ZJU:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4888 Problem Description Today is Yukari's n-th birthday. Ran and Chen hold a celebration party for her. Now comes the most import

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

HDOJ 4901 The Romantic Hero

DP....扫两遍组合起来 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 547    Accepted Submission(s): 217 Problem Description There is an old country and the king fell in love with a