hdu4430 Yukari's Birthday(二分)

31 篇文章 0 订阅
10 篇文章 0 订阅

题意:
给定一个n,计算满足x^1+x^2+……+x^r=n且使x*r最小(如果有多个,取r最小的那个)的x,r值。
由于2^40大于1e12,所以我们可以枚举r从1到40,但这样可能会超时,所以可以根据3^26大于2e12,我们可以枚举r从1到26,并单独计算x=2的情况。
对于给定的每个r,x^1+x^2+……+x^r这个式子就关于x单调递增,所以就可以用二分来做。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <stdlib.h>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#define msc(X) memset(X,-1,sizeof(X))
#define ms(X) memset(X,0,sizeof(X))
typedef long long LL;
using namespace std;
LL res,p2;
int p1;
void cal(LL n)
{
    if(((n+2)&(n+1))==0){
            int k=1;
            for(int i=2;i<=40;i++)
                if(!(n&(1ll<<i)))
                    {
                        k=i-1;
                        break;
                    }
            if(res>=(k<<1)) {
                res=(k<<1);
                p1=k;
                p2=2;
            }
        }
    for(int i=1;i<27;i++)
    {
        LL l=(LL)pow(2.0*n,1.0/(i+1.0)),
        r=(LL)pow((double)n,1.0/i)+5ll;
        while(l<r){
            LL m=(l+r)>>1;
            LL tmp=m;
            for(int j=1;j<i;j++)
                tmp=tmp*m+m;
            if(tmp<n) l=m+1;
            else if(tmp>n) r=m;
            else{
                if(res>m*i){
                    res=m*i;
                    p1=i;
                    p2=m;
                }
                break;
            }
        }
    }
}
int main(int argc, char const *argv[])
{
    LL n;
    while(scanf("%I64d",&n)!=EOF){
        res=1ll<<40;
        cal(n);
        cal(n-1);
        printf("%d %I64d\n",p1,p2 );
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值