HDU 4430 Yukari's Birthday (二分)

题意:有 n 个蜡烛,让你插到蛋糕上,每一层要插 k^i个根,第0层可插可不插,插的层数是r,让 r * k 尽量小,再让 r 尽量小,求r 和 k。

析:首先先列出方程来,一个是不插的一个是插的,比如插的是 sigam(0, r, k^i) = n,然后 r 比较小,可以枚举 r,然后二分求 k。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 10;
const LL mod = 1e12 + 10;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
LL n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c){
  return r >= 0 && r < n && c >= 0 && c < m;
}


LL judge(int mid, int r){
  LL sum = 0, t = mid;
  for(int i = 1; i <= r; ++i, t *= mid){
    sum += t;
    if(sum > mod) return mod;
  }
  return sum;
}

int main(){
  while(cin >> n){
    int ans = INF;
    int k = 0, rr;
    for(int i = 2; i < 41; ++i){
      int l = 1, r = (int)sqrt(n+0.5) + 1;
      int ttt = 0;
      while(l <= r){
        int mid = l + r >> 1;
        LL t = judge(mid, i);
        if(t == n || t == n-1){  ttt = mid;  break; }
        else if(t < n-1)  l = mid + 1;
        else r = mid - 1;
      }
      if(ttt > 0 && ttt * i < ans){
        ans = ttt * i;
        k = ttt;
        rr = i;
      }
    }
    if(k == 0 || k == 1)  printf("1 %I64d
", n-1);
    else  printf("%d %d
", rr, k);
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/7232226.html