Leetcode 483.最小好进制
最小好进制
对于给定的整数 n, 如果n的k(k>=2)进制数的所有数位全为1,则称 k(k>=2)是 n 的一个好进制。
以字符串的形式给出 n, 以字符串的形式返回 n 的最小好进制。
示例 1:
输入:"13"
输出:"3"
解释:13 的 3 进制是 111。
示例 2:
输入:"4681"
输出:"8"
解释:4681 的 8 进制是 11111。
示例 3:
输入:"1000000000000000000"
输出:"999999999999999999"
解释:1000000000000000000 的 999999999999999999 进制是 11。
提示:
- n的取值范围是 [3, 10^18]。
- 输入总是有效且没有前导 0。
本题是寻找一个数最小的good base。其定义是对于一个数y,其x进制表示为全1,则称x是y的good base。应该比较好理解,其实就是将y写成1+x+x^2+...+x^(n-1),就是一个等比数列求和,于是我们可以将其转化为y = (x^n - 1)/(x - 1),其中x>=2, 3<y<10^18,为了寻找最小的x,我们可以先来确定一下n的取值范围,很明显x越小n越大,所以当x=2时,n最大为log2(y+1)。从第三个例子可以看出来,当x=y-1时,n最小为2。所以有了n的取值范围我们就可以遍历所有可能的n,然后每次循环中y和n都是确定值,在对x使用二叉搜索确定其值即可。
另外一个需要注意的问题就是,因为本题中的数都比较大,所以要注意溢出问题,之前也做过一到这种题,可以使用java内置的BigInteger类进行处理。代码如下所示:
import java.math.BigInteger; class Solution {
public static String smallestGoodBase(String n) {
//现将字符串解析成long型数据
long s = Long.parseLong(n);
//对所有可能的指数n进行遍历
for (int max_e = (int) (Math.log(s) / Math.log(2)) + 1; max_e >= 2; max_e--) {
long low = 2, high = s, mid;
//进行二叉搜索,寻找最小的good base。
while (low <= high) {
mid = low + (high - low) / 2;
//一开始没有使用BigInteger,会报错
BigInteger left = BigInteger.valueOf(mid);
left = left.pow(max_e).subtract(BigInteger.ONE);
BigInteger right = BigInteger.valueOf(s).multiply(BigInteger.valueOf(mid).subtract(BigInteger.ONE));
int cmr = left.compareTo(right);
if (cmr == 0)
return String.valueOf(mid);
else if (cmr > 0)
high = mid - 1;
else
low = mid + 1;
}
}
return String.valueOf(s - 1);
}
}
Leetcode 483.最小好进制的更多相关文章
- [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 ...
- [leetcode] 提醒整理之进制
12. Integer to Roman Given an integer, convert it to a roman numeral. Input is guaranteed to be with ...
- 51 Nod 1116 K进制下的大数
1116 K进制下的大数 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 有一个字符串S,记录了一个大数,但不知这个大数是多少进制的,只知道这个数 ...
- LeetCode 405. Convert a Number to Hexadecimal (把一个数转化为16进制)
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s compl ...
- C#版 - Leetcode 504. 七进制数 - 题解
C#版 - Leetcode 504. 七进制数 - 题解 Leetcode 504. Base 7 在线提交: https://leetcode.com/problems/base-7/ 题目描述 ...
- [递归回溯] LeetCode 504七进制数(摸鱼版)
LeetCode 七进制数 前言: 这个就没什么好说的了 题目:略 步入正题 进位制转换 10 -n 余数加倒叙 没什么好讲的直接上七进制代码 偷个懒 10进位制转7 class Solution { ...
- ✡ leetcode 168. Excel Sheet Column Title 26进制数字 --------- java
Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...
- Leetcode 168 Excel Sheet Column Title 进制数转化
题意:将数字转化成excel表中的行中的项目 本质是10进制转化为26进制,但是在中间加入了一个不一样的操作,在每次操作前都需要n-- class Solution { public: string ...
- FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)
C Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status Pra ...
随机推荐
- Ubuntu 11.04 安装 cuda5.0
由于实验需要,于2016年10月15日再Ubuntu11.04安装cuda5.0,但是从网上查找Ubuntu11.04 只有对应的支持的cuda4 版本,cuda 5.0前面版本不支持IDE nisg ...
- Webpack 10分钟入门
可以说现在但凡开发Single page application,webpack是一个不可或缺的工具. WebPack可以看做是一个模块加工器,如上图所示.它做的事情是,接受一些输入,经过加工产生一些 ...
- 两个有序数列,求中间值 Median of Two Sorted Arrays
原题: There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the ...
- 解决sublime text 2总是在新窗口中打开文件(标签中打开)
在mac下不是很喜欢sublime text 2 总是在新窗口中打开文件,很麻烦,文件打多了,就会出现N多窗口,虽然可以直接打开当前目录可以解决,但有时候查看其它项目中的单个文件,就比较麻烦.百度一直 ...
- js类型判别大合集
1.typeof number,string,boolean,undefined,symbol,object,function 对象中除了函数为function,其他对象都判别为object, 缺陷: ...
- USACO08FEB Hotel
题目传送门 线段树维护区间 线段树结构体 struct zzz{ int l,r,mi; //l为以左端点的为起点的最长子串 //r为以右端点为终点的最长子串 //mi是区间内部的最长子串 }tree ...
- 27. Remove Element@python
Given an array nums and a value val, remove all instances of that value in-place and return the new ...
- Tcl/Cmds
- 通过composer安装阿里大于接口扩展
# 安装laravel阿里大鱼服务 composer require iscms/alisms-for-laravel laravel配置 # 注册服务 # 在config\app.php文件中找到P ...
- Survey lists 10 most innovative cities
From China Daily Beijing and Shanghai are among the 10 most innovative cities in the world, based on ...