<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zeros Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/zeros/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/zeros/</link>
	<description></description>
	<lastBuildDate>Wed, 07 Mar 2018 05:45:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>zeros Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/zeros/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 793. Preimage Size of Factorial Zeroes Function</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 04 Mar 2018 08:01:23 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Factorial]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[zeros]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1944</guid>

					<description><![CDATA[<p>Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! =&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/">花花酱 LeetCode 793. Preimage Size of Factorial Zeroes Function</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Let <code>f(x)</code> be the number of zeroes at the end of <code>x!</code>. (Recall that <code>x! = 1 * 2 * 3 * ... * x</code>, and by convention, <code>0! = 1</code>.)</p>
<p>For example, <code>f(3) = 0</code> because 3! = 6 has no zeroes at the end, while <code>f(11) = 2</code> because 11! = 39916800 has 2 zeroes at the end. Given <code>K</code>, find how many non-negative integers <code>x</code> have the property that <code>f(x) = K</code>.</p><pre class="crayon-plain-tag">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.</pre><p><strong>Note:</strong></p>
<ul>
<li><code>K</code> will be an integer in the range <code>[0, 10^9]</code>.</li>
</ul>
<p><strong>Idea:</strong></p>
<p>First we need to compute how many trailing zeros n! has.</p>
<p>See  <a href="http://zxi.mytechroad.com/blog/math/leetcode-172-factorial-trailing-zeroes/">花花酱 LeetCode 172. Factorial Trailing Zeroes</a> for details</p>
<p>It&#8217;s hard to say how many numbers have trailing zeros equals to K, but we can find the largest number p whose trailing zeros is K using binary search. (p+1)! has more than K trailing zeros. And do the same thing to find the largest number q whose trailing zeros is K &#8211; 1 using binary search.</p>
<p>Then we know that are exact p numbers 1,2,&#8230;,p whose trailing zeros are less or equal to K.</p>
<p>And exact q numbers 1, 2, &#8230;, q whose trailing zeros are less or equal to K &#8211; 1.</p>
<p>q + 1, q + 2, &#8230;, m (m &#8211; q numbers in total) the numbers with trailing zeros equal to K.</p>
<p><strong>Solution 1: Math + Binary Search</strong></p>
<p>Time complexity: O(log2(INT_MAX)*log5(INT_MAX))</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 3 ms
class Solution {
public:
  int preimageSizeFZF(int K) {
    return g(K) - g(K - 1);
  }
private:
  // Find the largest number l, that numZeros(l!) == K and numZeros((l+1)!) &gt; K
  int g(int K) {    
    int l = 0;
    int r = INT_MAX;
    while (l &lt; r) {
      int m = l + (r - l) / 2;
      int zeros = numZeros(m);
      if (zeros &lt;= K)
        l = m + 1;
      else
        r = m;
    }    
    return l;
  }
  
  int numZeros(int n) {
    return n &lt; 5 ? 0 : n / 5 + numZeros(n / 5);
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/">花花酱 LeetCode 793. Preimage Size of Factorial Zeroes Function</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
