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

这道题的题目名称非常的难懂,但是读了题目内容以后,就不难理解了,定义函数 f(x) 为 x! 的末尾0的个数,现在给了我们一个非负整数K,问使得 f(x)=K 成立的非负整数的个数。之前做过一道有关阶乘末尾零的个数的题 Factorial Trailing Zeroes,从那道里知道了末尾0其实是由2和5相乘为 10 得到的,而阶乘中2的数量远多于5的个数,所以 10 的个数就只取决于5的个数。需要注意的一点就是,像 25,125,这样的不只含有一个5的数字需要考虑进去。比如,24 的阶乘末尾有4个0,分别是 5,10,15,20 中的四个5组成的,而 25 的阶乘末尾就有6个0,分别是 5,10,15,20 中的各一个5,还有 25 中的两个5,所以共有六个5,那么就不存在其阶乘数末尾有5个0的数。还有一个很重要的规律需要发现,对于 20,21,22,23,24,这五个数的阶乘数末尾零的个数其实是相同的,都是有4个,因为它们包含的5的个数相同。而 19,18,17,16,15,这五个数末尾零个数相同,均为3。那么我们其实可以发现,每五个数,必会至少多出1个5,有可能更多。所以阶乘末尾零个数均为K个的x值,只有两种情况,要么是5,要么是0,这个规律得出来后,继续向着正确的解题方向前进。

基于之前那道题 Factorial Trailing Zeroes 的解法,可以知道如何快速求一个给定的数字阶乘末尾零的个数,那么只要找到了一个这样的数,其阶乘末尾零的个数等于K的话,那么就说明总共有5个这样的数,返回5,反之,如果找不到这样的数字,就返回0。像这种选一个 candidate 数字,再进行验证的操作,用二分搜索法就是极好的,属于博主的总结帖中 LeetCode Binary Search Summary 二分搜索法小结 的第四类,用子函数当作判断关系。首先要确定二分搜索法的范围,左边界很好确定,为0就行了,关键是来确定右边界,来分析一下,一个数字的阶乘末尾零个数为K,那么这个数字能有多大,就拿前面举的例子来说吧,末尾有4个0的最大数字是 24,有六个0的最大是 29,可以发现它们都不会超过 5*(K+1) 这个范围,所以这就是右边界,注意右边界可能会超过整型数范围,要用长整型来表示。那么之后就是经典的二分搜索法的步骤了,确定一个中间值 mid,然后调用子函数来计算 mid 的阶乘数末尾零的个数,用来和K比较大小,如果相等了,直接返回5,如果小于K,那么更新 left 为 mid+1,反之,更新 right 为 mid 即可,最终没找到的话,返回0即可,参见代码如下:

解法一:

class Solution {
public:
int preimageSizeFZF(int K) {
long left = , right = 5L * (K + );
while (left < right) {
long mid = left + (right - left) / ;
long cnt = numOfTrailingZeros(mid);
if (cnt == K) return ;
else if (cnt < K) left = mid + ;
else right = mid;
}
return ;
}
long numOfTrailingZeros(long x) {
long res = ;
for (; x > ; x /= ) {
res += x / ;
}
return res;
}
};

下面这种解法是把子函数融到了 while 循环内,使得看起来更加简洁一些,解题思路跟上面的解法一模一样,参见代码如下:

解法二:

class Solution {
public:
int preimageSizeFZF(int K) {
long left = , right = 5L * (K + );
while (left < right) {
long mid = left + (right - left) / , cnt = ;
for (long i = ; mid / i > ; i *= ) {
cnt += mid / i;
}
if (cnt == K) return ;
else if (cnt < K) left = mid + ;
else right = mid;
}
return ;
}
};

下面这种解法也挺巧妙的,也是根据观察规律推出来的,我们首先来看x为1到 25 的情况:

x:    1 2 3 4  6 7 8 9  11 12 13 14  16 17 18 19  21 22 23 24
f(x): 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4
g(x): 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

这里,f(x) 是表示 x! 末尾零的个数,而 g(x) = f(x) - f(x-1),其实还可以通过观察发现,f(x) = sum(g(x)).

再仔细观察上面的数字,发现 g(x) 有正值的时候都是当x是5的倍数的时候,那么来专门看一下x是5的倍数时的情况吧:

x:    5 10 15 20  30 35 40 45  55 60 65 70  80 85 90 95  105 110 115 120
g(x): 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

仔细观察上面的红色数字,g(x)=1 时,是5的倍数,g(x)=2 时,都是 25 的倍数,g(x)=3 时,是 125 的倍数,那么就有:

g(x) = 0     if x % 5 != 0,
g(x) >= 1    if x % 5 == 0,
g(x) >= 2   if x % 25 == 0.

如果继续将上面的数字写下去,就可以发现规律,g(x) 按照 1 1 1 1 x 的规律重复五次,第五次的时候x自增1。再继续观察:

当 x=25 时,g(x)=2,此时 K=5 被跳过了。

当 x=50 时,g(x)=2,此时 K=11 被跳过了。

当 x=75 时,g(x)=2,此时 K=17 被跳过了。

当 x=100 时,g(x)=2,此时 K=23 被跳过了。

当 x=125 时,g(x)=3,此时 K=29,30 被跳过了。

进一步,可以发现如下规律:

5(=1*5), 11(=6*1+5), 17(=6*2+5), 23(=6*3+5), 29(=6*4+5), 30(=6*5), 36(=31+5), 42(=31+6+5), 48(=31+6*2+5)

这些使得x不存在的K,出现都是有规律的,它们减去一个特定的基数 base 后,都是余5,而余 1,2,3,4 的,都是返回5。那么这个基 数base,实际是 1,6,31,156,...,是由 base = base * 5 + 1,不断构成的,通过这种不断对基数取余的操作,可以最终将K降为小于等于5的数,就可以直接返回结果了,参见代码如下:

解法三:

class Solution {
public:
int preimageSizeFZF(int K) {
if (K < ) return ;
int base = ;
while (base * + <= K) {
base = base * + ;
}
if (K / base == ) return ;
return preimageSizeFZF(K % base);
}
};

Github 同步地址:

https://github.com/grandyang/leetcode/issues/793

类似题目:

Factorial Trailing Zeroes

参考资料:

https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/

https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/discuss/117649/4ms-Java-Math-Solution

https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/discuss/117631/C++-O(logn)-math-solution-with-explanation

https://leetcode.com/problems/preimage-size-of-factorial-zeroes-function/discuss/117821/Four-binary-search-solutions-based-on-different-ideas

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

[LeetCode] Preimage Size of Factorial Zeroes Function 阶乘零的原像个数函数的更多相关文章

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

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

  3. 【leetcode】Preimage Size of Factorial Zeroes Function

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

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

  5. [LeetCode] Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  6. [LeetCode] 172. Factorial Trailing Zeroes 求阶乘末尾零的个数

    Given an integer n, return the number of trailing zeroes in n!. Example 1: Input: 3 Output: 0 Explan ...

  7. POJ 1401:Factorial 求一个数阶乘的末尾0的个数

    Factorial Time Limit: 1500MS   Memory Limit: 65536K Total Submissions: 15137   Accepted: 9349 Descri ...

  8. 【LeetCode】474. Ones and Zeroes 解题报告(Python)

    [LeetCode]474. Ones and Zeroes 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ ...

  9. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

随机推荐

  1. 横向滚动布局 white-space:nowrap

    float + 两层DOM实现 html <div class="container"> <div class="div1 clearfix" ...

  2. 【转】Reflector、reflexil、De4Dot、IL相关操作指令合集

    PS:CTRL+F 输入你需要的内容,可以快速查找页面上的内容. 名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加,执行溢出检查,并且将结果推送到计算堆栈上. ...

  3. Linux中执行C++程序

    参考:https://blog.csdn.net/qq_31125955/article/details/79343498 https://blog.csdn.net/weixin_35477207/ ...

  4. 数据库之数据库管理篇[mysql]

    管理数据库 1.mysql开闭使用篇 mariadb在Linux中首次进入mysql(因为此时还没有创建任何用户,mysql的root并不等效于linux中的root用户) sudo mysql 进入 ...

  5. luogu P5301 [GXOI/GZOI2019]宝牌一大堆

    传送门 wdnm又是打麻将 首先国土无双可以直接枚举哪种牌用了\(2\)次算贡献,然后\(7\)个对子可以把每种牌的对子贡献排序,取最大的\(7\)个,剩下的牌直接暴力枚举是不行的,考虑dp,设\(f ...

  6. android studio发布项目到github

    点击file   setting  ,打开对话框,如下,判断git是否安装成功 选择GitHub,填写github地址及密码 发布项目:

  7. Jmeter学习笔记03-元件作用域及执行顺序

    Jmeter可以通过鼠标拖拽来随意改变元件之间的顺序以及元件的层级关系,随着元件所在域的不同,在执行时候,会有不同效果 层级关系和元件类型决定了在测试计划中各元件的执行顺序. 1)元件的作用域: jm ...

  8. Win10上默认VS 2017以管理员身份运行

    Win10上的UAC虽然是个好东西,但是对于使用开发工作的技术人员来说有时候也挺麻烦.这里有一个让VS2017无论如何都以管理员身份运行的方法. 1.进入VS2017的安装目录:..\Microsof ...

  9. MinGW 编译 libaom 1.0.0 注意事项

    CMake 后不生成 config/aom_version.h 文件 需要手动编写 #define VERSION_MAJOR 1 #define VERSION_MINOR 0 #define VE ...

  10. 【easy】367. Valid Perfect Square 判断是不是平方数

    class Solution { public: bool isPerfectSquare(int num) { /* //方法一:蜜汁超时…… if (num < 0) return fals ...