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 ...
随机推荐
- [转]JVM堆和栈的区别
物理地址 堆的物理地址分配对对象是不连续的.因此性能慢些.在GC的时候也要考虑到不连续的分配,所以有各种算法.比如,标记-消除,复制,标记-压缩,分代(即新生代使用复制算法,老年代使用标记——压缩) ...
- poj 1694 An Old Stone Game 树形dp
//poj 1694 //sep9 #include <iostream> #include <algorithm> using namespace std; const in ...
- 计算机网络系列:2M的宽带指的是下载速度么?
本篇文章对于不懂网络的小白有点用处.避免以后闹笑话.当然.对大神来说.这都是常识了. 我相信非常多人都有过这个问题:我4M的宽带怎么下载速度才300kb/s啊啊啊.这坑爹的宽带. 我没学的时候我也会这 ...
- linked-list-cycle——链表、判断是否循环链表、快慢指针
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- 浅谈xss原理
近日,论坛上面XSS满天飞,各处都能够见到XSS的痕迹,前段时间论坛上面也出现了XSS的迹象.然后我等小菜不是太懂啊,怎么办?没办法仅仅有求助度娘跟谷歌这对情侣了. 能够说小菜也算懂了一些.不敢藏私, ...
- 让 Logo "飞" 出屏幕
让 Logo "飞" 出屏幕 推荐序 本文介绍了一种思路,即利用矢量工具来生成动画的关键代码,然后进一步制作成完整的动画效果,感谢作者授权转载. 作者介绍:一缕殇流化隐半边冰霜 ...
- 关于HTTP1.1的长连接
HTTP是一个构建在传输层的TCP协议之上的应用层的协议,在这个层的协议,是一种网络交互须要遵守的一种协议规范. HTTP1.0的短连接 HTTP 1.0规定浏览器与server仅仅保持短暂的连接.浏 ...
- Apache Qpid消息通讯模型和消息地址简介
Broker知识准备 Broker内置两种节点类型:一种是 queue,一种是 topic. 1. queue 节点能够缓存消息,直到被读取走为止.queue节点满足两个重要的 PTP 通信的特征, ...
- git push & git pull 推送/拉取指定分支
https://blog.csdn.net/litianze99/article/details/52452521
- android 提示
1.Toast: Toast toast=new Toast(context); Toast.makeText(context, text, duration);//返回值为Toast toast.s ...