题意:已知有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; }