zoukankan      html  css  js  c++  java
  • hdu 4430二分枚举

    枚举r,二分K就可以了,不过还是需要注意细节,比较数据类型之类的,不然容易错。

    /*
     * hdu4430/win.cpp
     * Created on: 2012-10-26
     * Author    : ben
     */
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cmath>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <set>
    #include <map>
    #include <stack>
    #include <string>
    #include <vector>
    #include <deque>
    #include <list>
    #include <functional>
    #include <numeric>
    #include <cctype>
    using namespace std;
    typedef long long LL;
    
    LL judge(LL n, int r, int k) {
        LL sum = 0, t = 1;
        while(r--) {
            t *= k;
            sum += t;
            if(sum > n) {
                return 1;
            }
        }
        if(sum == n || sum == n - 1) {
            return 0;
        }
        if(sum > n) {
            return 1;
        }
        return -1;
    }
    
    int bsearchk(int r, LL n) {
        int low = 1, high = 10000000;
        while(low <= high) {
            int mid = (low + high) / 2;
            LL ret = judge(n, r, mid);
            if(ret > 0) {
                high = mid - 1;
            }else if(ret == 0) {
                return mid;
            }else {
                low = mid + 1;
            }
        }
        return -1;
    }
    
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("data.in", "r", stdin);
    #endif
        LL n;
        while(scanf("%I64d", &n) == 1) {
            LL ansrk = n - 1;
            LL ansr = 1, ansk = n - 1;
            for(int r = 2; r <= 70; r++) {
                int k = bsearchk(r, n);
                if(k >= 2) {
                    LL temp = (LL)r * k;
                    if(temp < ansrk) {
                        ansrk = temp;
                        ansr = r;
                        ansk = k;
                    }
                }
            }
            printf("%I64d %I64d\n", ansr, ansk);
        }
        return 0;
    }
  • 相关阅读:
    检测一个对象方法是否存在
    非堵塞 延迟脚本 高性能
    移动开发 相关 备忘
    元素透明 渐变函数
    cookie
    media query 单位
    前端入门可参考《如何教会非计算机专业的女友写代码》
    JQ判断复选框是否选中
    聊天记录
    JS获取文本值
  • 原文地址:https://www.cnblogs.com/moonbay/p/2741926.html
Copyright © 2011-2022 走看看