HDU 4430 Yukari's Birthday (二分)
2024-08-23 22:49:58
题意:有 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", n-1);
else printf("%d %d\n", rr, k);
}
return 0;
}
最新文章
- Django (2)
- 模拟登陆115网盘(MFC版)
- Unix环境编程之定时、信号与中断
- java记事本
- (转) IsPostBack的用法
- ECSHOP如何增加红包序列号字符
- mysql按ID排序(转)
- 用js把图片做的富有动态感,并对以后需要用着的属性进行封装
- 【转】W3C中国与百度联合组织移动网页加速技术研讨会
- MyBatis generator配置 overwrite 文件覆盖失效
- German Collegiate Programming Contest 2018​ A. Attack on Alpha-Zet
- VS2010、VS2012、VS2013、VS2015、VS2017各版本产品激活秘钥
- grid - 它和flex布局有何区别?
- 不同eclipse版本的git库使用
- bootsrap Collapse用法
- 【BZOJ2328】 [HNOI2011]赛车游戏
- php登陆绑定手机验证码使用阿里大于接口
- 内网环境NTP服务及时间同步(CentOS6.x)配置和部署
- zoj 1760 查找
- istringstream和ostringstream的实现