[LeetCode] 483. Smallest Good Base 最小的好基数
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1.
Now given a string representing n, you should return the smallest good base of n in string format.
Example 1:
Input: "13"
Output: "3"
Explanation: 13 base 3 is 111.
Example 2:
Input: "4681"
Output: "8"
Explanation: 4681 base 8 is 11111.
Example 3:
Input: "1000000000000000000"
Output: "999999999999999999"
Explanation: 1000000000000000000 base 999999999999999999 is 11.
Note:
- The range of n is [3, 10^18].
- The string representing n is always valid and will not have leading zeros.
这道题让我们求最小的好基数,定义了一个大于等于2的基数k,如果可以把数字n转化为各位都是1的数,那么就称这个基数k是好基数。通过看题目中的三个例子,应该大致可以理解题意了吧。如果我们用k表示基数,m表示转为全1数字的位数,那么数字n就可以拆分为:
n = 1 + k + k^2 + k^3 + ... + k^(m-1)
这是一个等比数列,中学数学的内容吧,利用求和公式可以表示为 n = (k^m - 1) / (k - 1)。我们的目标是求最小的k,那么仔细观察这个式子,在n恒定的情况,k越小则m却大,就是说上面的等式越长越好。下面我们来分析m的取值范围,题目中给了n的范围,是 [3, 10^18]。由于k至少为2,n至少为3,那么肯定至少有两项,则 m>=2。再来看m的上限该如何求?其实也不难,想要m最大,k就要最小,k最小是2,那么m最大只能为 log2(n + 1),数字n用二进制表示的时候可拆分出的项最多。但这道题要求变换后的数各位都是1,那么我们看题目中最后一个例子,可以发现,当 k=n-1 时,一定能变成 11,所以实在找不到更小的情况下就返回 n-1。
下面我们来确定k的范围,由于k至少为2,那么就可以根据下面这个不等式来求k的上限:
n = 1 + k + k^2 + k^3 + ... + k^(m-1) > k^(m-1)
解出 k < n^(1 / (m-1)),其实我们也可以可以通过 n < k^m - 1 来求出k的准确的下限,但由于是二分查找法,下限直接使用2也没啥问题。分析到这里,那么解法应该已经跃然纸上了,我们遍历所有可能的m值,然后利用二分查找法来确定k的值,对每一个k值,我们通过联合m值算出总和 sum,然后跟n来对比即可,参见代码如下:
class Solution {
public:
string smallestGoodBase(string n) {
long long num = stol(n);
for (int i = log(num + ) / log(); i >= ; --i) {
long long left = , right = pow(num, 1.0 / (i - )) + ;
while (left < right) {
long long mid = left + (right - left) / , sum = ;
for (int j = ; j < i; ++j) {
sum = sum * mid + ;
}
if (sum == num) return to_string(mid);
if (sum < num) left = mid + ;
else right = mid;
}
}
return to_string(num - );
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/483
参考资料:
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 483. Smallest Good Base 最小的好基数的更多相关文章
- [LeetCode] Smallest Good Base 最小的好基数
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- Leetcode 483. Smallest Good Base
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- 483. Smallest Good Base
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- [LeetCode] 910. Smallest Range II 最小区间之二
Given an array A of integers, for each integer A[i] we need to choose either x = -K or x = K, and ad ...
- [LeetCode] 908. Smallest Range I 最小区间
Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...
- [Swift]LeetCode483. 最小好进制 | Smallest Good Base
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- Binary Search-483. Smallest Good Base
For an integer n, we call k>=2 a good base of n, if all digits of n base k are 1. Now given a str ...
- [LeetCode] 727. Minimum Window Subsequence 最小窗口子序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequenceof ...
- [LeetCode] 632. Smallest Range Covering Elements from K Lists 覆盖K个列表元素的最小区间
You have k lists of sorted integers in ascending order. Find the smallest range that includes at lea ...
随机推荐
- LeetCode 752:打开转盘锁 Open the Lock
题目: 你有一个带有四个圆形拨轮的转盘锁.每个拨轮都有10个数字: '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' .每个拨轮可以自由旋转:例如把 ' ...
- LeetCode20——有效的括号
在记事本中写算法题和在纸上写其实感觉差不多,反正是不能进行调试.想起某高手的话,写代码要做到“人机合一”,写高级语言时(指的是 C 和 C++)脑海中要知道当前写的代码对应的反汇编代码,也就是要深入了 ...
- Spring Cloud Ribbon客户端负载均衡(四)
序言 Ribbon 是一个客户端负载均衡器(Nginx 为服务端负载均衡),它赋予了应用一些支配 HTTP 与 TCP 行为的能力,可以得知,这里的客户端负载均衡也是进程内负载均衡的一种.它在 Spr ...
- 【UOJ#75】【UR #6】智商锁(矩阵树定理,随机)
[UOJ#75][UR #6]智商锁(矩阵树定理,随机) 题面 UOJ 题解 这种题我哪里做得来啊[惊恐],,, 题解做法:随机\(1000\)个点数为\(12\)的无向图,矩阵树定理算出它的生成树个 ...
- LeetCode 1292. Maximum Side Length of a Square with Sum Less than or Equal to Threshold
题目 我是按照边进行二分的 class Solution { public: int sum[100005]; int a[305][305]; int maxSideLength(vector< ...
- 来迟了,用Python助你叠猫猫,抢618大红包!
目录: 0 引言 1 环境 2 需求分析 3 前置准备 4 逛店铺流程回顾 5 代码全景展示 6 总结 0 引言 最近叠猫猫的活动可真是十分的火爆,每天小伙伴们为了合猫猫忙的可谓是如火如荼.为啥要叠猫 ...
- Linux入门——注意事项
Linux入门——注意事项 摘要:本文主要说明了在使用Linux操作系统时,需要注意的问题. 严格区分大小写 和Windows不同,Linux是严格区分大小写的,包括文件名和目录名.命令.命令选项.配 ...
- CentOS7 vsftp 安装与配置(视频教程)
(双击全屏播放) 1.安装vsftpd yum install -y vsftpd 2.编辑ftp配置文件 vi /etc/vsftpd/vsftpd.conf anonymous_enable=NO ...
- sublime_text运行python ctrl+b运行的界面隐藏了怎么重新调出来恢复显示?
sublime_text运行python ctrl+b运行的界面隐藏了怎么重新调出来恢复显示?搜索了下都是说怎么隐藏的,隐藏后怎么恢复显示的没找到看进程还在运行,但调不出来看运行结果了,console ...
- php动态拼接变量名,可变变量,动态变量,使用花括号,使用两个$符
php动态拼接变量名,可变变量,动态变量,使用花括号,使用两个$符方式一:使用花括号,前缀部分不需要用单引号$nums10 = 100;$xxx*${bcount.$nums10}['m54']/$n ...