<?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>big integer Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/big-integer/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/big-integer/</link>
	<description></description>
	<lastBuildDate>Sun, 26 Feb 2023 06:07:36 +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>big integer Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/big-integer/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2575. Find the Divisibility Array of a String</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-2575-find-the-divisibility-array-of-a-string/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-2575-find-the-divisibility-array-of-a-string/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 26 Feb 2023 06:07:28 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[big integer]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9967</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;string&#160;word&#160;of length&#160;n&#160;consisting of digits, and a positive integer&#160;m. The&#160;divisibility array&#160;div&#160;of&#160;word&#160;is an integer array of length&#160;n&#160;such that: div[i] = 1&#160;if the&#160;numeric value&#160;of&#160;word[0,...,i]&#160;is divisible&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-2575-find-the-divisibility-array-of-a-string/">花花酱 LeetCode 2575. Find the Divisibility Array of a String</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>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;string&nbsp;<code>word</code>&nbsp;of length&nbsp;<code>n</code>&nbsp;consisting of digits, and a positive integer&nbsp;<code>m</code>.</p>



<p>The&nbsp;<strong>divisibility array</strong>&nbsp;<code>div</code>&nbsp;of&nbsp;<code>word</code>&nbsp;is an integer array of length&nbsp;<code>n</code>&nbsp;such that:</p>



<ul><li><code>div[i] = 1</code>&nbsp;if the&nbsp;<strong>numeric value</strong>&nbsp;of&nbsp;<code>word[0,...,i]</code>&nbsp;is divisible by&nbsp;<code>m</code>, or</li><li><code>div[i] = 0</code>&nbsp;otherwise.</li></ul>



<p>Return<em>&nbsp;the divisibility array of</em><em>&nbsp;</em><code>word</code>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word = "998244353", m = 3
<strong>Output:</strong> [1,1,0,0,0,1,1,0,0]
<strong>Explanation:</strong> There are only 4 prefixes that are divisible by 3: "9", "99", "998244", and "9982443".
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> word = "1010", m = 10
<strong>Output:</strong> [0,1,0,1]
<strong>Explanation:</strong> There are only 2 prefixes that are divisible by 10: "10", and "1010".
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= n &lt;= 10<sup>5</sup></code></li><li><code>word.length == n</code></li><li><code>word</code>&nbsp;consists of digits from&nbsp;<code>0</code>&nbsp;to&nbsp;<code>9</code></li><li><code>1 &lt;= m &lt;= 10<sup>9</sup></code></li></ul>



<h2><strong>Solution: Big Integer Math</strong></h2>



<p>r = (r * 10 + word[i]) % m</p>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;int&gt; divisibilityArray(string word, int m) {
    vector&lt;int&gt; ans;
    long long r = 0;
    for (char c : word) {
      r = (r * 10 + c - '0') % m;
      ans.push_back(r == 0);
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-2575-find-the-divisibility-array-of-a-string/">花花酱 LeetCode 2575. Find the Divisibility Array of a String</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-2575-find-the-divisibility-array-of-a-string/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 1404. Number of Steps to Reduce a Number in Binary Representation to One</title>
		<link>https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/</link>
					<comments>https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 05 Apr 2020 07:22:53 +0000</pubDate>
				<category><![CDATA[Bit]]></category>
		<category><![CDATA[big integer]]></category>
		<category><![CDATA[binary string]]></category>
		<category><![CDATA[bit]]></category>
		<category><![CDATA[medieum]]></category>
		<category><![CDATA[O(n)]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=6578</guid>

					<description><![CDATA[<p>Given a number&#160;s&#160;in their binary representation. Return the number of steps to reduce it to 1 under the following rules: If the current number is&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/">花花酱 LeetCode 1404. Number of Steps to Reduce a Number in Binary Representation to One</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>Given a number&nbsp;<code>s</code>&nbsp;in their binary representation. Return the number of steps to reduce it to 1 under the following rules:</p>



<ul><li>If the current number is even, you have to divide it by 2.</li><li>If the current number is odd, you have to add 1 to it.</li></ul>



<p>It&#8217;s guaranteed that you can always reach to one for all testcases.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1101"
<strong>Output:</strong> 6
<strong>Explanation:</strong> "1101" corressponds to number 13 in their decimal representation.
Step 1) 13 is odd, add 1 and obtain 14.&nbsp;
Step 2) 14 is even, divide by 2 and obtain 7.
Step 3) 7 is odd, add 1 and obtain 8.
Step 4) 8 is even, divide by 2 and obtain 4.&nbsp; 
Step 5) 4 is even, divide by 2 and obtain 2.&nbsp;
Step 6) 2 is even, divide by 2 and obtain 1.&nbsp; 
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "10"
<strong>Output:</strong> 1
<strong>Explanation:</strong> "10" corressponds to number 2 in their decimal representation.
Step 1) 2 is even, divide by 2 and obtain 1.&nbsp; 
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> s = "1"
<strong>Output:</strong> 0
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= s.length&nbsp;&lt;= 500</code></li><li><code>s</code>&nbsp;consists of characters &#8216;0&#8217; or &#8216;1&#8217;</li><li><code>s[0] == '1'</code></li></ul>



<h2><strong>Solution: Simulation</strong></h2>



<p>Time complexity: O(n)<br>Space complexity: O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numSteps(string s) {
    int ans = 0;
    int carry = 0;
    // The highest bit must be 1, 
    // process to the 2nd highest bit
    for (int i = s.length() - 1; i &gt; 0; --i) {
      if (s[i] - '0' + carry == 1) {
        ans += 2; // odd: +1, even: / 2
        carry = 1; // always has a carry
      } else {
        ans += 1; // even: / 2
        // carray remains e.g. 1 + 1 = 10, or 0 + 0 = 00
      }
    }
    // If there is a carry, then it's even, one more step.
    return ans + carry;
  }
};</pre>
</div></div>



<div class="responsive-tabs">
<h2 class="tabtitle">Python3</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag"># Author: Huahua
class Solution:
  def numSteps(self, s: str) -&gt; int:
    ans, carry = 0, 0
    for i in range(1, len(s)):
      if ord(s[-i]) - ord('0') + carry == 1:
        ans += 2
        carry = 1
      else:
        ans += 1
    return ans + carry</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/">花花酱 LeetCode 1404. Number of Steps to Reduce a Number in Binary Representation to One</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/bit/leetcode-1404-number-of-steps-to-reduce-a-number-in-binary-representation-to-one/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 43. Multiply Strings</title>
		<link>https://zxi.mytechroad.com/blog/simulation/leetcode-43-multiply-strings/</link>
					<comments>https://zxi.mytechroad.com/blog/simulation/leetcode-43-multiply-strings/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 26 Jul 2018 14:40:16 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[Simulation]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[big integer]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[simulation]]></category>
		<category><![CDATA[string]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=3300</guid>

					<description><![CDATA[<p>Problem Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, also represented as a string. Example 1: Input: num1 = "2", num2 = "3"&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-43-multiply-strings/">花花酱 LeetCode 43. Multiply Strings</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<div class="question-description__3U1T">
<div>
<h1><strong>Problem</strong></h1>
<p>Given two non-negative integers <code>num1</code> and <code>num2</code> represented as strings, return the product of <code>num1</code> and <code>num2</code>, also represented as a string.</p>
<p><strong>Example 1:</strong></p>
<pre class="crayon:false"><strong>Input:</strong> num1 = "2", num2 = "3"
<strong>Output:</strong> "6"</pre>
<p><strong>Example 2:</strong></p>
<pre class="crayon:false "><strong>Input:</strong> num1 = "123", num2 = "456"
<strong>Output:</strong> "56088"
</pre>
<p><strong>Note:</strong></p>
<ol>
<li>The length of both <code>num1</code> and <code>num2</code> is &lt; 110.</li>
<li>Both <code>num1</code> and <code>num2</code> contain only digits <code>0-9</code>.</li>
<li>Both <code>num1</code> and <code>num2</code> do not contain any leading zero, except the number 0 itself.</li>
<li>You <strong>must not use any built-in BigInteger library</strong> or <strong>convert the inputs to integer</strong> directly.</li>
</ol>
</div>
</div>
<h1><strong>Solution: Simulation</strong></h1>
<p>Simulate multiplication one digit at a time.</p>
<p>Time complexity: O(l1*l2)</p>
<p>Space complexity: O(l1 + l2)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  string multiply(string num1, string num2) {
    const int l1 = num1.length();
    const int l2 = num2.length();
    string ans(l1 + l2, '0');
    for (int i = l1 - 1; i &gt;= 0; --i)
      for (int j = l2 - 1; j &gt;= 0; --j) {
        int sum = (ans[i + j + 1] - '0') + (num1[i] - '0') * (num2[j] - '0');        
        ans[i + j + 1] = (sum % 10) + '0';
        ans[i + j] += sum / 10;
      }
    for (int i = 0; i &lt; ans.length(); ++i)
      if (ans[i] != '0' || i == ans.length() - 1) return ans.substr(i);
    return "";
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/simulation/leetcode-43-multiply-strings/">花花酱 LeetCode 43. Multiply Strings</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/simulation/leetcode-43-multiply-strings/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
