793. Preimage Size of Factorial Zeroes Function
Let f(x)
be the number of zeroes at the end of x!
. (Recall that x! = 1 * 2 * 3 * ... * x
, and by convention, 0! = 1
.)
For example, f(3) = 0
because 3! = 6 has no zeroes at the end, while f(11) = 2
because 11! = 39916800 has 2 zeroes at the end. Given K
, find how many non-negative integers x
have the property that f(x) = K
.
Example 1:
Input: K = 0
Output: 5
Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes. Example 2:
Input: K = 5
Output: 0
Explanation: There is no x such that x! ends in K = 5 zeroes.
Note:
K
will be an integer in the range[0, 10^9]
.
Approach #1: Bianry Serach.
class Solution {
public:
int preimageSizeFZF(int K) {
return (int)(searchNum(K) - searchNum(K-1));
} private:
long findNumOfZeros(long x) {
long res = 0;
for (; x > 0; x /= 5) {
res += x / 5;
}
return res;
} long searchNum(int x) {
long l = 0, r = 5 * (x + 1);
while (l <= r) {
long m = l + (r - l) / 2;
long count = findNumOfZeros(m);
if (count > x) r = m - 1;
else l = m + 1;
}
return r;
}
};
Runtime: 0 ms, faster than 100.00% of C++ online submissions for Preimage Size of Factorial
Analysis:
step1:
the number of zero with factorial's result equal to the number of 5 in factorial.
eg:
5! = 1 * 2 * 3 * 4 * 5 = 120
11! = 1 * 2 *...* 5 *... * 9 * 10 *...* 11 = 39916800
25! = 1 * 2 *...* 5 *... * 9 * 10 *...* 15 * ... * 20 * .... * 25 || in this case the number of 5 equal to 25 / 5 + 25 / 25 + 25 / 125
so wecan get the faction of findNumOfZeros();
long findNumOfZeros(long x) {
long res = 0;
for (; x > 0; x /= 5) {
res += x / 5;
}
return res;
}
step 2:
we can use binary search to find the num1 (range with [0, 5*(K + 1)]) whose factorial with K zeros and num2 whose factorial with K - 1 zeros.
long searchNum(int x) {
long l = 0, r = 5 * (x + 1);
while (l <= r) {
long m = l + (r - l) / 2;
long count = findNumOfZeros(m);
if (count > x) r = m - 1;
else l = m + 1;
}
return r;
}
finally find the answer num1 - num2.
793. Preimage Size of Factorial Zeroes Function的更多相关文章
- [LeetCode] Preimage Size of Factorial Zeroes Function 阶乘零的原像个数函数
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...
- [Swift]LeetCode793. 阶乘函数后K个零 | Preimage Size of Factorial Zeroes Function
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...
- 74th LeetCode Weekly Contest Preimage Size of Factorial Zeroes Function
Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...
- 【leetcode】Preimage Size of Factorial Zeroes Function
题目如下: 解题思路:<编程之美>中有一个章节是不要被阶乘吓倒,里面讲述了“问题一:给定一个整数N,那么N的阶乘末尾有多少个0呢?例如N = 10, N! = 362800,N! 的末尾有 ...
- (python走过的坑)OpenCV中错误opencv-3.3.1\modules\highgui\src\window.cpp:339: error: (-215) size.width>0 && size.height>0 in function cv::imshow
第一次在python中使用OpenCV(cv2),运行时报错opencv-3.3.1\modules\highgui\src\window.cpp:339: error: (-215) size.wi ...
- error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'
用Python打开图像始终提示错误 error: OpenCV(4.1.1) C:\projects\opencv-python\opencv\modules\highgui\src\window.c ...
- opencv报错 error: (-215) size.width>0 && size.height>0 in function cv::imshow
使用opencv读取摄像头并且显示事出现此问题: 后来发现是图像为空时的错误,加入: if(!frame.empty()) imshow("video",frame); 完整的代码 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- leetcode 学习心得 (4)
645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...
随机推荐
- Apatch常用的commons工具包介绍
1.Commons BeanUtils http://jakarta.apache.org/commons/beanutils/index.html 说明:针对Bean的一个工具集.由于Bean往往是 ...
- 【转载】lvs为何不能完全替代DNS轮询
上一篇文章"一分钟了解负载均衡的一切"引起了不少同学的关注,评论中大家争论的比较多的一个技术点是接入层负载均衡技术,部分同学持这样的观点: 1)nginx前端加入lvs和keepa ...
- Intel Naming Strategy--2
http://en.wikipedia.org/wiki/Intel_Corporation#Naming_strategy Naming strategy[edit] In 2006, Intel ...
- Hibernate 配置C3P0 连接池
第一步在hibernate.cfg.xml配置 <!-- 连接池 --> <property name="hibernate.connection.provider_cla ...
- 2015-03-12---外观模式,建造者模式(附代码),观察者模式(附代码),boost库应用
今天白天主要看了boost库的应用,主要是经常使用的一些库,array,bind,function,regex,thread,unordered,ref,smartpointers库,晚上看了看设计模 ...
- CPU维修技术
中央处理单元(Central Process Unit)简称CPU,是电脑的核心部件,负责处理和运算电脑内部所有数据.因其在电脑中的地位相当重要,所以一旦发生故障就会造成很严重的后果. [技术66]开 ...
- 深入理解JVM:HotSpot虚拟机对象探秘
对象的创建 java是一门面向对象的语言.在Java程序执行过程中无时无刻有Java对象被创建出来.在语言层面上,创建对象(克隆.反序列化)一般是一个newkeyword而已,而在虚拟机中,对象的创建 ...
- 【Java】Java代码经典错误清单
一.String 对照 == 和 equals.详细描写叙述例如以下 "=="操作符的作用 1)用于基本数据类型的比較,例如以下: byte(字节) 8 -128 - 127 0 ...
- spring cloud 服务消费
Ribbon Ribbon可以在通过客户端中配置的ribbonServerList服务端列表去轮询访问以达到均衡负载的作用. 当Ribbon与Eureka联合使用时,ribbonServerList会 ...
- (MySQL里的数据)通过Sqoop Import Hive 里 和 通过Sqoop Export Hive 里的数据到(MySQL)
Sqoop 可以与Hive系统结合,实现数据的导入和导出,用户需要在 sqoop-env.sh 中添加HIVE_HOME的环境变量. 具体,见我的如下博客: hadoop2.6.0(单节点)下Sqoo ...