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 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
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef __int64 LL;
using namespace std;

const int MAX=1000+10;
LL n;

LL cal(LL k,LL r){
    LL sum=1,ans=0;
    for(int i=1;i<=r;++i){
    	if(n/sum<k)return n+1;
        sum=sum*k;//sum*k可能会溢出
        ans+=sum;
        if(ans>n)return ans;//ans大于n直接返回
    }
    return ans;
}

LL search(LL i){
    LL l=2,r=n;
    while(l<=r){
        LL mid=(l+r)>>1;
        LL sum=cal(mid,i);
        if(sum == n-1 || sum == n)return mid;
        if(sum<n-1)l=mid+1;
        else r=mid-1;
    }
    return -1;
}

int main(){
    while(~scanf("%I64d",&n)){
        LL a=1,b=n-1;
        for(LL i=2;i<=60;++i){//枚举r然后对k进行二分查找,因为k>?=2,2^60>10^12,所以i只要枚举到60即可
            LL k=search(i);
            if(k != -1 && i*k<a*b){a=i,b=k;}
        }
        printf("%I64d %I64d\n",a,b);
    }
    return 0;
}

hdu4430之枚举+二分,布布扣,bubuko.com

时间: 2024-10-03 14:03:13

hdu4430之枚举+二分的相关文章

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

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

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.但是会有精度误差,貌似有解决的办法,但是觉得没法理解.后面看了

poj 3977 Subset 枚举+二分

首先分成一半2^17和2^18,并且把其中一半变成相反数,然后枚举一半二分查找另一半,在找到的位置前后也找找. 这里用到了二级排序,有很多细节要处理,不多说了. 巨坑的一个地方就是,不能用系统的abs,要自己手写,简直坑死.. #include<cstdio> #include<algorithm> #include<iostream> #include<map> using namespace std; typedef long long ll; stru

POJ 3977Subset(枚举+二分)

Subset Time Limit: 30000MS   Memory Limit: 65536K Total Submissions: 1562   Accepted: 261 Description Given a list of N integers with absolute values no larger than 1015, find a non empty subset of these numbers which minimizes the absolute value of

Codeforces 496D Tennis Game 枚举+二分

题目链接:点击打开链接 题意: 给定n场比赛. 下面n个数字:表示该场是1获胜还是2获胜. 1.胜利者获得一分. 2.若已经决出整个赛季的胜负则比赛不会继续. 3.设要赢得这个赛季需要赢有s局,每局先获得t分的选手胜利. 问: 找出所有的(s,t)组合使得给定的n场比赛记录合法. 输出要排序. 枚举t. a数组存第一个人赢的哪些场次. b数组存第二个人赢的哪些场次. 设赢t分为一句.则判断 第一个人再赢t分是第几场,第二个人再赢t分是第几场. 显然先赢得t分的人赢了这场. 这样同时跑2个人的场数

51nod1010(枚举+二分)

题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1010 题意:中文题诶- 思路:求第一个比 x (1<=x<=1e18)大或者等于的数y, 且y的因子只有2, 3, 5,即y=pow(2, i)*pow(3, j)*pow(5, k); 那么显然我们可以通过枚举i, j, k求出所有由2, 3, 5因子构成的数,并将其存入数组中,再二分查找数组中第一个大于等于x的数即为答案: 代码: 1 #include

HDU 4282 A very hard mathematic problem --枚举+二分(或不加)

题意:问方程X^Z + Y^Z + XYZ = K (X<Y,Z>1)有多少个正整数解 (K<2^31) 解法:看K不大,而且不难看出 Z<=30, X<=sqrt(K), 可以枚举X和Z,然后二分找Y,这样的话不把pow函数用数组存起来的话好像会T,可以先预处理出1~47000的2~30次幂,这样就不会T了. 但是还可以简化,当Z=2时,X^2+Y^2+2XY = (X+Y)^2 = K, 可以特判下Z= 2的情况,即判断K是否为平方数,然后Z就可以从3开始了,这样的话X^