• HDU


    题意:已知有n个蜡烛,过生日在蛋糕上摆蜡烛,将蜡烛围成同心圆,每圈个数为ki,蛋糕中心最多可摆一个蜡烛,求圈数r和看,条件为r*k尽可能小的情况下,r尽可能小。

    分析:n最大为1012,k最少为2,假设k为2,r最多为40,因此枚举r,二分k。

    需要两个剪枝防止爆LL,

    在计算ans=k1+k2+……+kr的过程中

    (1)当kr>n时,break,并向左区间继续搜索

    (2))当ans>n时,break,并向左区间继续搜索

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<cmath>
    #include<iostream>
    #include<sstream>
    #include<iterator>
    #include<algorithm>
    #include<string>
    #include<vector>
    #include<set>
    #include<map>
    #include<stack>
    #include<deque>
    #include<queue>
    #include<list>
    #define lowbit(x) (x & (-x))
    const double eps = 1e-8;
    inline int dcmp(double a, double b){
        if(fabs(a - b) < eps) return 0;
        return a > b ? 1 : -1;
    }
    typedef long long LL;
    typedef unsigned long long ULL;
    const int INT_INF = 0x3f3f3f3f;
    const int INT_M_INF = 0x7f7f7f7f;
    const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
    const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
    const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
    const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
    const int MOD = 1e9 + 7;
    const double pi = acos(-1.0);
    const int MAXN = 10000 + 10;
    const int MAXT = 10000 + 10;
    using namespace std;
    LL n;
    struct Node{
        int r;
        LL k;
        bool operator < (const Node &rhs)const{
            return r * k < rhs.r * rhs.k || (r * k == rhs.r * rhs.k && r < rhs.r);
        }
    }num[50];
    LL deal(LL k, int r){
        LL ans = 0;
        LL tmp = 1;
        bool ok = true;
        for(int i = 1; i <= r; ++i){
            if(n / tmp < k){
                return -1;
            }
            tmp *= k;
            ans += tmp;
            if(ans > n){
                return -1;
            }
        }
        return ans;
    }
    LL solve(int r){
        LL L = 2, R = n;
        while(L <= R){
            LL mid = L + (R - L) / 2;
            LL tmp = deal(mid, r);
            if(tmp == n || tmp == n - 1) return mid;
            if(tmp == -1) R = mid - 1;
            else if(tmp < n - 1) L = mid + 1;
        }
        return -1;
    }
    int main(){
        while(scanf("%lld", &n) == 1){
            int cnt = 0;
            for(int r = 1; r <= 40; ++r){
                LL k = solve(r);
                if(k == -1) continue;
                num[cnt].r = r;
                num[cnt++].k = k;
            }
            sort(num, num + cnt);
            printf("%d %lld
    ", num[0].r, num[0].k);
        }
        return 0;
    }
    

      

  • 相关阅读:
    JVM调优之Tomcat启动加速(二)
    JVM调优(一)
    安装SqlServer的时候性能计数器注册表配置单元一致性失败的解决办法
    VS2013崩溃,无法打开项目的解决方案
    C#实现函数默认值和C#4.0实现默认值
    日志管理
    PXE网络装机服务
    NFS网络共享搭建
    NFS共享
    linux文件系统文件删除并恢复
  • 原文地址:https://www.cnblogs.com/tyty-Somnuspoppy/p/7242154.html
Copyright © 2020-2023  润新知