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 ...
随机推荐
- 具体解释window.history
Window.history保存用户在一个会话期间的站点訪问记录,用户每次訪问一个新的URL即创建一个新的历史记录. history.go().history.back().history.forwa ...
- cocos2d-x改底层之获取UIListView的实际内容大小
实际项目中UI界面中常常会用到UIListView.大多会在CocoStudio中直接加入这个控件. 可是在使用中发现了一些坑和功能缺乏,然后就看了一下底层的逻辑,发现略微改一下底层就能够满足需求,所 ...
- 【微信支付】分享一个失败的案例 跨域405(Method Not Allowed)问题 关于IM的一些思考与实践 基于WebSocketSharp 的IM 简单实现 【css3】旋转倒计时 【Html5】-- 塔台管制 H5情景意识 --飞机 谈谈转行
[微信支付]分享一个失败的案例 2018-06-04 08:24 by stoneniqiu, 2744 阅读, 29 评论, 收藏, 编辑 这个项目是去年做的,开始客户还在推广,几个月后发现服务器已 ...
- CSP:使用CryptoAPI解码X509证书内容
微软的CryptoAPI提供了一套解码X509证书的函数,一个X509证书解码之后,得到一个PCCERT_CONTEXT类型的结构体指针. 通过该结构体,我们就能够获取想要的证书项和属性等. X509 ...
- Enum to String 一般用法
目录 一.Enum Review 二.使用name()方法转换为String 三.使用toString()方法转换为String 四.使用成员属性转换为String 一.Enum Review J ...
- openwrt network 初始化
openwrt 烧写完成之后, 第一次启动会设置 network 的相关参数, 如 ip地址, mac地址, 等. 这里跟踪一下启动之后直到网络参数设置的过程. /sbin/init -> pr ...
- openwrt 模拟i2c驱动(一)
一:加载i2c driver kmod-i2c-core................................................ I2C support kmod-i2c-al ...
- Codeforces Round #253 (Div. 2)——Borya and Hanabi
题目连接 题意: n表示有n个卡片.每一个卡片有一种颜色和一个数字(共五种不同的颜色和五个不同的数字). 事先知道每种卡片有几张.可是不知道详细的位置. 问须要几次提示就能够知道全部卡片的位置都在哪里 ...
- 定时任务Timer
一.Timer介绍 java.util.Timer java.util.TimerTask Timer是一个定时器类,通过该类可以为指定的定时任务进行配置.TimerTask类是一个定时任务类,该类实 ...
- Duplicate Observed Data
在翻看<重构-改善既有代码的设计>这本经典的书,书中就介绍了一个重构方法--Duplicate Observed Data 复制被监视数据的重构方法,使用这种方法能够使界面和对数据的操作隔 ...