Codeforces #361C. Mike and Chocolate Thieves 二分 数学
2016-07-07 17:50
387 查看
题目
题目链接:http://codeforces.com/contest/689/problem/C题目来源:Codeforces#361
题解
m内等比数列的个数要等于输入的n且最小。由于个数是单调的,可以二分答案,计算等比数列的个数。
计算的时候枚举公比q,然后n/q3就是个数(整数除)。
由于n−−√3的规模不大,所以时间足够,上界没仔细算,不过8×1015肯定可以,用2就够了。
二分出的答案计算等比数列个数后不等于n就是−1。
代码
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <stack> #include <queue> #include <string> #include <vector> #include <set> #include <map> #define fi first #define se second using namespace std; typedef long long LL; typedef pair<int,int> PII; // head const int N = 3e5 + 5; LL a ; LL ck(LL x) { LL sum = 0; for (int i = 2; a[i] <= x; i++) { sum += x / a[i]; } return sum; } int main() { for (int i = 2; i < N; i++) { a[i] = (LL)i * i * i; } LL n; while (scanf("%I64d", &n) == 1) { LL l = 0, r = a[N-1], ans; while (l <= r) { LL mid = (l + r) / 2; if (ck(mid) < n) { l = mid + 1; } else { ans = mid; r = mid - 1; } } printf("%I64d\n", ck(ans) == n ? ans : -1); } return 0; }
相关文章推荐
- linux 线程创建和结束
- DialogFragment实现带数字倒计时和小圆点进度的圆形进度条
- 设计模式之代理模式(Proxy )
- 参加阿里云栖大会Tech Insight,不可错过的6大理由
- YYModel 源码解读 总结
- freshman
- 统计二进制中1的个数
- 拒绝繁琐,Volley框架的简单封装
- Scalaz(46)- scalaz-stream 基础介绍
- oracle中把不同字段中的值整理成一句话输出
- Android 局部带阴影的网络加载框
- IP地址和子网掩码的关系
- linux下操作mysql
- ASP.NET弹出提示点击确定之后再跳转页面的方法
- Linux 2.6.0 中select()的主要代码片断——The C10M Problem
- css3改变滚动条样式-webkit-
- eclispe 无法启动调试 cannot connect to VM
- 每日一算法,递归算法应用
- hive 1.2.1的常量大坑bug
- Impala中最大连接数的设置