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 ...
随机推荐
- [转]gzip,bzip2,tar,zip命令使用方法详解
原文:http://blog.chinaunix.net/uid-20779720-id-2547669.html 1 gzipgzip(1) 是GNU的压缩程序.它只对单个文件进行压缩.基本用法如下 ...
- [MDX] Build a Custom Provider Component for MDX Deck
MDX Deck is a great library for building slides using Markdown and JSX. Creating a custom Providerco ...
- LRUCache 具体解释
LRU的基本概念: LRU是Least Recently Used的缩写,最近最少使用算法. Java 实现LRUCache 1.基于LRU的基本概念,为了达到按最近最少使用排序.能够选择HashMa ...
- CodeForces 321A Ciel and Robot(数学模拟)
题目链接:http://codeforces.com/problemset/problem/321/A 题意:在一个二维平面中,開始时在(0,0)点,目标点是(a.b),问能不能通过反复操作题目中的指 ...
- android的armeabi和armeabi-v7a
在ANE中如果SDK调用了so库,则需要把so库放到ANE下Android-ARM/lib/armeabi (调试模式)或者 armeabi-v7a(发行模式)下. 可以贴个ADT代码说明问题: // ...
- Yelp面试题目
题目:FizzBuzz 从stdin得到数字N(<10^7),然后从打印出从1到N的数字.输出到stdout,假设数字是3的倍数的话就仅仅打印"Buzz",假设数字是5的倍数 ...
- Expression Tree 学习笔记(一)
大家可能都知道Expression Tree是.NET 3.5引入的新增功能.不少朋友们已经听说过这一特性,但还没来得及了解.看看博客园里的老赵等诸多牛人,将Expression Tree玩得眼花缭乱 ...
- 20160222.CCPP体系具体解释(0032天)
程序片段(01):宽字符.c+字符串与内存四区.c 内容概要:宽窄字符 ///宽字符.c #include <stdio.h> #include <stdlib.h> #inc ...
- MySQL搭建系列之多实例
所谓多实例.就是在一台server上搭建.执行多个MySQL实例,每一个实例使用不同的服务port.通过不同的socket监听:物理上,每一个实例拥有独立的參数配置文件及数据库. 通常情况下.一台se ...
- mtk机型的一次救砖经历
在recovery里清除了data,cache,system三个分区,没有刷机,重启到bootloader,准备另刷recovery. 有急事走开了,回来时发现手机黑屏,无论什么组合键都没反应,以为是 ...