Leetcode 483 - Smallest Good Base(二分+枚举)
来源:互联网 发布:大学生程序员 编辑:程序博客网 时间:2024/05/17 02:46
题意
给一个数n,让我们确定一个n的进制base,使n在base的表示下是全1。即13在进制为3的表示111。
思路
我们假设x在进制为a的表示下为n个1,即
在n确定的情况下,我们可以二分一下a,如果a还没有加到
又由于求最小的base,于是我们的n从大往小枚举。
代码
#define LL __int128class Solution {public: LL stringToLL(string s) { LL x = 0; for (int i = 0; i < s.size(); i++) x *= 10, x += s[i] - '0'; return x; } int judge(LL x, LL a, int n) { LL s = 0, pp = 1; for (int i = 0; i < n; i++) { if (s > x) return 1; pp *= i ? a : 1; s += pp; } return s > x ? 1 : (s == x ? 0 : -1); } string LLToString(LL x) { string s = ""; while (x) { s.push_back(x % 10 + '0'); x /= 10; } reverse(s.begin(), s.end()); return s; } string smallestGoodBase(string n) { LL x = stringToLL(n); for (int i = 64; i >= 0; i--) { LL l = 2, r = 1e18, m, ans; while (l <= r) { m = l + (r - l >> 1); if (judge(x, m, i) == 0) return LLToString(m); else if (judge(x, m, i) == 1) r = m - 1; else l = m + 1; } } return LLToString(0); }};
0 0
- Leetcode 483 - Smallest Good Base(二分+枚举)
- leetcode 483. Smallest Good Base 二分查找
- LeetCode 483 Smallest Good Base 解题报告
- LeetCode 483 Smallest Good Base 题解
- leetCode 483. Smallest Good Base
- [leetcode]483. Smallest Good Base
- leetcode题解-483. Smallest Good Base
- [Leetcode] 483. Smallest Good Base 解题报告
- 483. Smallest Good Base
- 483. Smallest Good Base
- 483. Smallest Good Base
- Smallest Good Base
- 483. Smallest Good Base
- 483. Smallest Good Base
- leetcode483. Smallest Good Base 的一些思考
- CF-51C - Three Base Stations(二分或枚举优化)
- Leetcode 378 - Kth Smallest Element in a Sorted Matrix(二分+杨氏矩阵)
- POJ:2718 Smallest Difference(暴力枚举)
- ABAP CA CO CS等操作符
- Python学习笔记4
- 多媒体(视频播放,拍照,录像)
- 关于Java组件开发:一个概念框架(组图)
- Vue.js 系列教程 2:组件,Props,Slots
- Leetcode 483 - Smallest Good Base(二分+枚举)
- 对勾选的下拉选择进行同步选择
- 数据读取/写入——环型缓存区
- Java Thread多线程详解及用法解析
- AMF简介
- Linux网络流量查看工具
- Android平台抓取native crash log
- Java多线程的创建及启动
- 使用Retrofit和Rxjava对联网进行简单封装