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. 九度OJ 1120:全排列 (DFS)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4471 解决:1139 题目描述: 给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列. 我们假设对于小写字母有'a' < ...

  2. 对小程序的网络请求的封装 wx.request 接收参数修改

    wepy-mall/wxRequest.js at master · dyq086/wepy-mall https://github.com/dyq086/wepy-mall/blob/master/ ...

  3. 在DuiLib中使用MFC类

    比如,想在DuiLib里使用MFC中的CInternetSession 首先,将Project Properties->General->Use of MFC设置为Use MFC in a ...

  4. 阿里妈妈-RAP项目的实践(3)

    接下来,我们就把我们的代码运用到项目中,因为我们前台是有jquery,后台管理系统是用angularjs mock在这两种的调用方式不一样,所以我就用nginx的proxy_pass 来代理 我在项目 ...

  5. 【R】R语言生成随机数

    1.概述 作为一种语言进行统计分析,R有一个随机数生成各种统计分布功能的综合性图书馆.R语言可以针对不同的分布,生成该分布下的随机数.其中,有许多常用的个分布可以直接调用.本文简单介绍生成常用分布随机 ...

  6. cocoapod使用

    什么是cocoapod CocoaPods是用于方便使用第三方开源库的管理工具,减少我们对第三方库的各种配置. 安装教程参考: CocoaPods的介绍.安装.使用和原理 Cocoapod安装使用 第 ...

  7. 【转】JBoss Web和 Tomcat的区别

    转载于:http://www.verydemo.com/demo_c202_i780.html JBoss Web和 Tomcat的区别 在Web2.0的浪潮中,各种页面技术和框架不断涌现,为服务器端 ...

  8. 局域网 —— VLAN

    0. network 简单分类 LAN:Local Area Network,局域网: WLAN:Wireless LAN(Local Area Network), VLAN:Virtual LAN( ...

  9. python 文件与文件夹常见操作以及os.walk的用法

    文件操作: In [34]: import os In [35]: os.rename("hello[复件].py","hello111.py")       ...

  10. BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心

    BZOJ_4010_[HNOI2015]菜肴制作_拓扑排序+贪心 Description 知名美食家小 A被邀请至ATM 大酒店,为其品评菜肴. ATM 酒店为小 A 准备了 N 道菜肴,酒店按照为菜 ...