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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 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 ...

  4. 【leetcode】Preimage Size of Factorial Zeroes Function

    题目如下: 解题思路:<编程之美>中有一个章节是不要被阶乘吓倒,里面讲述了“问题一:给定一个整数N,那么N的阶乘末尾有多少个0呢?例如N = 10, N! = 362800,N! 的末尾有 ...

  5. (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 ...

  6. 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 ...

  7. opencv报错 error: (-215) size.width>0 && size.height>0 in function cv::imshow

    使用opencv读取摄像头并且显示事出现此问题: 后来发现是图像为空时的错误,加入: if(!frame.empty()) imshow("video",frame); 完整的代码 ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. leetcode 学习心得 (4)

    645. Set Mismatch The set S originally contains numbers from 1 to n. But unfortunately, due to the d ...

随机推荐

  1. 关于结构体的具体解说,C、C++中的差别

    1. C.C++内置的类型分两种,一种是基本数据类型.一种是复合数据类型.此处我们要讲的结构体便是复合数据类型. 先来讨论一下结构体存在的意义吧.或许你觉得主要的数据类型就够了,为什么还要有结构题这样 ...

  2. Bean property XX&#39; is not writable or has an invalid setter method

    刚刚搞spring.property注入时遇到这个问题,百度一下.非常多人说是命名或者get set方法不一致的问题,可是这个我是知道的.写的时候也注意到这些.所以应该不是这个问题.以为是xml头写的 ...

  3. VS2013带来的&quot;新特性&quot;

    VS2013除了引入"Bootstrap"库以外,最大的变化就是.net Framework 4.5下面的都不支持了.也就是说,假设不把.net库升级成.net Framework ...

  4. IE浏览器部分版本不支持background-size属性问题

    background-size是CSS3新增的属性,但是IE8以下还是不支持,可以通过滤镜来实现这样的一个效果 background-size:contain; // 缩小图片来适应元素的尺寸(保持像 ...

  5. openwrt gstreamer实例学习笔记(二.gstreamer 的 Element)

    对程序员来说,GStreamer 中最重要的一个概念就是 GstElement 对象.该对象是构建一个媒体管道的基本块.所有上层(high-level)部件都源自GstElement对象.任何一个解码 ...

  6. TestNG – Dependency Test

    转自:http://www.mkyong.com/unittest/testng-tutorial-7-dependency-test/ In TestNG, we use dependOnMetho ...

  7. php浏览次数累加代码

    <?php $count=0; if(file_exists("count.txt")) //判断是否存在count.txt文件 { $count=file_get_cont ...

  8. 常用的sql命令

    1 mysql创建数据库 create database [database name]; 2 创建表 create table [table name]([first column name] [f ...

  9. yarn笔记

    常用命令: 创建项目:yarn init 安装依赖包:yarn == yarn install 添加依赖包:yarn add Yarn命令列表 命令 操作 参数 标签 yarn add 添加依赖包 包 ...

  10. VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3

    C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes input ...