1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/求出所有子序列的能量和 [find-the-sum-of-the-power-of-all-subsequences].html
2024-03-22 16:17:08 +08:00

72 lines
5.0 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个长度为 <code>n</code>&nbsp;的整数数组&nbsp;<code>nums</code>&nbsp;和一个 <strong></strong>&nbsp;整数&nbsp;<code>k</code>&nbsp;</p>
<p>一个整数数组的 <strong>能量</strong>&nbsp;定义为和 <strong>等于</strong>&nbsp;<code>k</code>&nbsp;的子序列的数目。</p>
<p>请你返回 <code>nums</code>&nbsp;中所有子序列的 <strong>能量和</strong>&nbsp;</p>
<p>由于答案可能很大,请你将它对&nbsp;<code>10<sup>9</sup> + 7</code>&nbsp;<strong>取余</strong>&nbsp;后返回。</p>
<p>&nbsp;</p>
<p><strong class="example">示例 1</strong></p>
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
<p><strong>输入:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [1,2,3], k = 3 </span></p>
<p><strong>输出:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 6 </span></p>
<p><strong>解释:</strong></p>
<p>总共有&nbsp;<code>5</code>&nbsp;个能量不为 0 的子序列:</p>
<ul>
<li>子序列&nbsp;<code>[<em><strong>1</strong></em>,<em><strong>2</strong></em>,<em><strong>3</strong></em>]</code>&nbsp;<code>2</code>&nbsp;个和为&nbsp;<code>3</code>&nbsp;的子序列:<code>[1,2,<strong><em>3</em></strong>]</code><code>[<strong><em>1</em></strong>,<strong><em>2</em></strong>,3]</code>&nbsp;</li>
<li>子序列&nbsp;<code>[<em><strong>1</strong></em>,2,<em><strong>3</strong></em>]</code>&nbsp;<code>1</code>&nbsp;个和为&nbsp;<code>3</code>&nbsp;的子序列:<code>[1,2,<strong><em>3</em></strong>]</code>&nbsp;</li>
<li>子序列&nbsp;<code>[1,<em><strong>2</strong></em>,<em><strong>3</strong></em>]</code>&nbsp;<code>1</code>&nbsp;个和为&nbsp;<code>3</code>&nbsp;的子序列:<code>[1,2,<strong><em>3</em></strong>]</code>&nbsp;</li>
<li>子序列&nbsp;<code>[<em><strong>1</strong></em>,<em><strong>2</strong></em>,3]</code>&nbsp;&nbsp;<code>1</code>&nbsp;个和为&nbsp;<code>3</code>&nbsp;的子序列:<code>[<strong><em>1</em></strong>,<strong><em>2</em></strong>,3]</code>&nbsp;</li>
<li>子序列&nbsp;<code>[1,2,<em><strong>3</strong></em>]</code>&nbsp;&nbsp;<code>1</code>&nbsp;个和为&nbsp;<code>3</code>&nbsp;的子序列:<code>[1,2,<strong><em>3</em></strong>]</code>&nbsp;</li>
</ul>
<p>所以答案为&nbsp;<code>2 + 1 + 1 + 1 + 1 = 6</code>&nbsp;</p>
</div>
<p><strong class="example">示例 2</strong></p>
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
<p><strong>输入:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [2,3,3], k = 5 </span></p>
<p><strong>输出:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 4 </span></p>
<p><strong>解释:</strong></p>
<p>总共有&nbsp;<code>3</code>&nbsp;个能量不为 0 的子序列:</p>
<ul>
<li>子序列&nbsp;<code>[<em><strong>2</strong></em>,<em><strong>3</strong></em>,<em><strong>3</strong></em>]</code>&nbsp;有 2 个子序列和为&nbsp;<code>5</code>&nbsp;<code>[<strong><em>2</em></strong>,3,<strong><em>3</em></strong>]</code>&nbsp;<code>[<strong><em>2</em></strong>,<strong><em>3</em></strong>,3]</code>&nbsp;</li>
<li>子序列&nbsp;<code>[<em><strong>2</strong></em>,3,<em><strong>3</strong></em>]</code>&nbsp;有 1 个子序列和为&nbsp;<code>5</code>&nbsp;<code>[<strong><em>2</em></strong>,3,<strong><em>3</em></strong>]</code>&nbsp;</li>
<li>子序列&nbsp;<code>[<em><strong>2</strong></em>,<em><strong>3</strong></em>,3]</code>&nbsp;有 1 个子序列和为 <code>5</code>&nbsp;<code>[<strong><em>2</em></strong>,<strong><em>3</em></strong>,3]</code>&nbsp;</li>
</ul>
<p>所以答案为&nbsp;<code>2 + 1 + 1 = 4</code>&nbsp;</p>
</div>
<p><strong class="example">示例 3</strong></p>
<div class="example-block" style="border-color: var(--border-tertiary); border-left-width: 2px; color: var(--text-secondary); font-size: .875rem; margin-bottom: 1rem; margin-top: 1rem; overflow: visible; padding-left: 1rem;">
<p><strong>输入:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> nums = [1,2,3], k = 7 </span></p>
<p><strong>输出:</strong> <span class="example-io" style="font-family: Menlo,sans-serif; font-size: 0.85rem;"> 0 </span></p>
<p><strong>解释:</strong>不存在和为 <code>7</code>&nbsp;的子序列,所以 <code>nums</code>&nbsp;的能量和为&nbsp;<code>0</code>&nbsp;</p>
</div>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= n &lt;= 100</code></li>
<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
<li><code>1 &lt;= k &lt;= 100</code></li>
</ul>