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:

  1. The range of n is [3, 10^18].
  2. The string representing n is always valid and will not have leading zeros.
 
Approach #1:
class Solution {
public:
string smallestGoodBase(string n) {
unsigned long long tn = (unsigned long long)stoll(n);
unsigned long long x = 1;
for (int i = 62; i >= 1; --i) {
if ((x<<i) < tn) {
unsigned long long temp = solve(tn, i);
if (temp != 0) return to_string(temp);
}
}
return to_string(tn-1);
}
private:
unsigned long long solve(unsigned long long num, int d) {
double tn = (double) num;
unsigned long long r = (unsigned long long)(pow(tn, 1.0/d)+1);
unsigned long long l = 1;
while (l <= r) {
unsigned long long sum = 1;
unsigned long long cur = 1;
unsigned long long m = l + (r - l) / 2;
for (int i = 1; i <= d; ++i) {
cur *= m;
sum += cur;
}
if (sum == num) return m;
if (sum < num) l = m + 1;
else r = m - 1;
}
return 0;
}
};
Runtime: 4 ms, faster than 49.59% of C++ online submissions for Smallest Good Base.

 Analysis:

The input can be stored in a long long int, here I use unsigned long long int for a larger range. We need to find k, for 1+k^1+k^2+k^3+...+k^d=n. The smallest possible base is k=2, with has the longest possible representation, i.e., largest d. So, to find the smallest base means to find the longest possible representation "11111....1" based on k. As n<=10^18, so n<(1<<62). We iterate the length of the representation from 62 to 2 (2 can always be valid, with base=n-1), and check whether a given length can be valid.

For a given length d, we use binary search to check whether there is a base k which satisfies 1+k^1+k^2+...k^d=n. The left limit is 1, and the right limit is pow(n,1/d)+1, i.e., the d th square root of n. The code is shown below.

come from: https://leetcode.com/problems/smallest-good-base/discuss/96590/3ms-AC-C%2B%2B-long-long-int-%2B-binary-search

483. Smallest Good Base的更多相关文章

  1. [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 ...

  2. 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 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  7. leetcode 几道题目

    是周六晚上的几道题,晚上11点半,睡的早,起不来! 494. Target Sum 分析:看完这题,看到数据范围,长度20,枚举就是1<<20 = 1e6, 然后单次20,总共就是2e8, ...

  8. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  9. Leetcode problems classified by company 题目按公司分类(Last updated: October 2, 2017)

    All LeetCode Questions List 题目汇总 Sorted by frequency of problems that appear in real interviews. Las ...

随机推荐

  1. VLC For Android Ubuntu14.04编译环境搭建

    VLC多媒体播放器(英语:VLC media player,最初为VideoLAN Client.是VideoLAN计划的开放源码多媒体播放器.)支持众多音频与视频解码器及文件格式,并支持DVD影音光 ...

  2. 【转】安卓逆向实践5——IDA动态调试so源码

    之前的安卓逆向都是在Java层上面的,但是当前大多数App,为了安全或者效率问题,会把一些重要功能放到native层,所以这里通过例子记录一下使用IDA对so文件进行调试的过程并对要点进行总结. 一. ...

  3. ByteBuf(图解1)

    目录 源码工程 写在前面 Netty ByteBuf 优势 手动获取与释放ByteBuf 自动获取和释放 ByteBuf 方式一:TailHandler 自动释放 方式二:SimpleChannelI ...

  4. 6 Maven聚合与集成

    Maven的聚合特性能够把项目的各个模块聚合在一起构件,而Maven的继承特性能够帮助抽取各个模块相同的依赖和插件等配置,简化POM的同时,还能促进各个模块配置的一致性.     1.聚合     为 ...

  5. 有关 java 不定参数

    不定参数实际为数组参数的一种写法而已,本质上与数组参数完全相同 //1.数组参数函数 public static int sum(int[] values) { } //2.不定参数函数 不定参数只能 ...

  6. 基于sys文件系统的LED驱动的移植【原创】

    基于RK3188平台LED驱动程序的移植的移植.如有不正确之处,欢迎大家指点. 本文的LED驱动程序不是通过打开设备节点来访问和控制LED的,是通过sys文件系统来控制LED. 板子上有四盏灯以及对应 ...

  7. UVA10689 Yet another Number Sequence —— 斐波那契、矩阵快速幂

    题目链接:https://vjudge.net/problem/UVA-10689 题解: 代码如下: #include <iostream> #include <cstdio> ...

  8. ReentrantReadWriteLock读写锁实现分析

    排他锁在同一时刻只允许一个线程进行访问,而读写锁在同一时刻允许多个读线程访问,但是在写线程访问时,所有的读线程和其他的写线程均被阻塞.读写锁内部维护了一对锁,一个读锁和一个写锁,通过分离读锁和写锁,使 ...

  9. valid No such filter: 'drawtext'"

    libfreetype is missing. You'll have to rebuild FFmpeg with this library or disable overlays. --enabl ...

  10. source insight 快捷键

    source insight 返回上一视图 快捷键:ALT+, 浏览项目符号:F7 全局浏览查找某个名称开头的函数或结构体等