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:
Kwill 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 ...
随机推荐
- 使用crontab定时执行脚本时别忘了输出重定向>
原文:https://blog.csdn.net/solmyr_biti/article/details/50683279 -------------------------------------- ...
- 为什么一个目录里放超过十个Mp4文件会导致资源管理器和播放程序变卡变慢?
最近<鬼吹灯之精绝古城>大火,我也下载了剧集放在移动硬盘里. 起初还没事,当剧集超过十个时发现资源管理器变慢了,表现为上方的绿条总是在闪动前进,给文件改名都缓慢无比. 当剧集超过十五个时, ...
- ci中 chrome对favicon.ico请求,导致log报错 解决方法
chrome浏览器会自动对favicon.ico进行请求,所以当没有为网站添加 favicon.ico 时会出现404的页面错误在实际开发过程中,CI框架 打开 ERROR log,会一直显示ERRO ...
- LOCAL_CFLAGS参数说明
1.-Wall 是打开警告开关 2.-O 代表默认优化,可选:-O0不优化,-O1低级优化,-O2中级优化,-O3高级优化,-Os代码空间优化 3.-g 是生成调试信息,生成的可执行文件具有和源代码关 ...
- html5 式程序员表白
html5 式程序员表白 王海庆 于 星期三, 04/06/2014 - 00:44 提交 今天是个好日子,2014年5月20日,表白的最佳时机,虽说孩子已经四岁.结婚已经五年.可是也不能够偷懒.于是 ...
- Java多线程面试题归纳
1.多线程有哪几种实现方法?举个样例说明下线程的同步. (1)Java多线程有两种实现方式:继承Thread类和实现Runnable接口,Thread就是实现了Runnable接口. 两个最简单的线程 ...
- eureka高可用注册中心
Eureka高可用注册中心 两个配置文件: application-peer1.properties application-peer2.properties 都需要加上 eureka.client. ...
- 文件管理中心iOS版简介
App Store地址:https://itunes.apple.com/cn/app/id1023365565?mt=8 文件管理中心-装机必备的文件管家,专业的rar-zip 解压工具,局域网看片 ...
- SequenceFileInputFormat区别TextInputFormat
通过InputFormat,Hadoop可以: l 检查MapReduce输入数据的正确性: l 将输入数据切分为逻辑块InputSplit,这些块会分配给Ma ...
- app发布流程
在app上架之前做两件事(instruments,profile): 1.代码静态分析:不用运行程序,直接检测代码有没有潜在的一些内存泄漏 2.动态分析:a l loctions/leaks 内存溢出 ...