diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/README.md b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/README.md index 54cdb0646eff4..374b0ef58b281 100644 --- a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/README.md +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/README.md @@ -97,32 +97,237 @@ tags: -### 方法一 +### 方法一:动态规划 + +题目需要我们在给定数组 $\textit{nums}$ 中找到所有子序列 $\textit{S}$,然后计算每个 $\textit{S}$ 的每个子序列 $\textit{T}$ 的和等于 $\textit{k}$ 的方案数。 + +我们定义 $f[i][j]$ 表示前 $i$ 个数构成的若干个子序列中,每个子序列的子序列和等于 $j$ 的方案数。初始时 $f[0][0] = 1$,其余位置均为 $0$。 + +对于第 $i$ 个数 $x$,有以下三种情况: + +1. 不在子序列 $\textit{S}$ 中,此时 $f[i][j] = f[i-1][j]$; +1. 在子序列 $\textit{S}$,但不在子序列 $\textit{T}$ 中,此时 $f[i][j] = f[i-1][j]$; +1. 在子序列 $\textit{S}$,且在子序列 $\textit{T}$ 中,此时 $f[i][j] = f[i-1][j-x]$。 + +综上,状态转移方程为: + +$$ +f[i][j] = f[i-1][j] \times 2 + f[i-1][j-x] +$$ + +最终答案为 $f[n][k]$。 + +时间复杂度 $O(n \times k)$,空间复杂度 $O(n \times k)$。其中 $n$ 为数组 $\textit{nums}$ 的长度,而 $k$ 为给定的正整数。 #### Python3 ```python - +class Solution: + def sumOfPower(self, nums: List[int], k: int) -> int: + mod = 10**9 + 7 + n = len(nums) + f = [[0] * (k + 1) for _ in range(n + 1)] + f[0][0] = 1 + for i, x in enumerate(nums, 1): + for j in range(k + 1): + f[i][j] = f[i - 1][j] * 2 % mod + if j >= x: + f[i][j] = (f[i][j] + f[i - 1][j - x]) % mod + return f[n][k] ``` #### Java ```java - +class Solution { + public int sumOfPower(int[] nums, int k) { + final int mod = (int) 1e9 + 7; + int n = nums.length; + int[][] f = new int[n + 1][k + 1]; + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + for (int j = 0; j <= k; ++j) { + f[i][j] = (f[i - 1][j] * 2) % mod; + if (j >= nums[i - 1]) { + f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod; + } + } + } + return f[n][k]; + } +} ``` #### C++ ```cpp +class Solution { +public: + int sumOfPower(vector& nums, int k) { + const int mod = 1e9 + 7; + int n = nums.size(); + int f[n + 1][k + 1]; + memset(f, 0, sizeof(f)); + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + for (int j = 0; j <= k; ++j) { + f[i][j] = (f[i - 1][j] * 2) % mod; + if (j >= nums[i - 1]) { + f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod; + } + } + } + return f[n][k]; + } +}; +``` +#### Go + +```go +func sumOfPower(nums []int, k int) int { + const mod int = 1e9 + 7 + n := len(nums) + f := make([][]int, n+1) + for i := range f { + f[i] = make([]int, k+1) + } + f[0][0] = 1 + for i := 1; i <= n; i++ { + for j := 0; j <= k; j++ { + f[i][j] = (f[i-1][j] * 2) % mod + if j >= nums[i-1] { + f[i][j] = (f[i][j] + f[i-1][j-nums[i-1]]) % mod + } + } + } + return f[n][k] +} +``` + +#### TypeScript + +```ts +function sumOfPower(nums: number[], k: number): number { + const mod = 10 ** 9 + 7; + const n = nums.length; + const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0)); + f[0][0] = 1; + for (let i = 1; i <= n; ++i) { + for (let j = 0; j <= k; ++j) { + f[i][j] = (f[i - 1][j] * 2) % mod; + if (j >= nums[i - 1]) { + f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod; + } + } + } + return f[n][k]; +} +``` + + + + + + + +### 方法二:动态规划(优化) + +方法一中的状态转移方程中,$f[i][j]$ 的值只与 $f[i-1][j]$ 和 $f[i-1][j-x]$ 有关,因此我们可以优化第一维空间,从而将空间复杂度优化为 $O(k)$。 + +时间复杂度 $O(n \times k)$,空间复杂度 $O(k)$。其中 $n$ 为数组 $\textit{nums}$ 的长度,而 $k$ 为给定的正整数。 + + + +#### Python3 + +```python +class Solution: + def sumOfPower(self, nums: List[int], k: int) -> int: + mod = 10**9 + 7 + f = [1] + [0] * k + for x in nums: + for j in range(k, -1, -1): + f[j] = (f[j] * 2 + (0 if j < x else f[j - x])) % mod + return f[k] +``` + +#### Java + +```java +class Solution { + public int sumOfPower(int[] nums, int k) { + final int mod = (int) 1e9 + 7; + int[] f = new int[k + 1]; + f[0] = 1; + for (int x : nums) { + for (int j = k; j >= 0; --j) { + f[j] = (f[j] * 2 % mod + (j >= x ? f[j - x] : 0)) % mod; + } + } + return f[k]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int sumOfPower(vector& nums, int k) { + const int mod = 1e9 + 7; + int f[k + 1]; + memset(f, 0, sizeof(f)); + f[0] = 1; + for (int x : nums) { + for (int j = k; j >= 0; --j) { + f[j] = (f[j] * 2 % mod + (j >= x ? f[j - x] : 0)) % mod; + } + } + return f[k]; + } +}; ``` #### Go ```go +func sumOfPower(nums []int, k int) int { + const mod int = 1e9 + 7 + f := make([]int, k+1) + f[0] = 1 + for _, x := range nums { + for j := k; j >= 0; j-- { + f[j] = f[j] * 2 % mod + if j >= x { + f[j] = (f[j] + f[j-x]) % mod + } + } + } + return f[k] +} +``` +#### TypeScript + +```ts +function sumOfPower(nums: number[], k: number): number { + const mod = 10 ** 9 + 7; + const f: number[] = Array(k + 1).fill(0); + f[0] = 1; + for (const x of nums) { + for (let j = k; ~j; --j) { + f[j] = (f[j] * 2) % mod; + if (j >= x) { + f[j] = (f[j] + f[j - x]) % mod; + } + } + } + return f[k]; +} ``` diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/README_EN.md b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/README_EN.md index 0069c359275d9..62d347cb5428b 100644 --- a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/README_EN.md +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/README_EN.md @@ -95,32 +95,237 @@ tags: -### Solution 1 +### Solution 1: Dynamic Programming + +The problem requires us to find all subsequences $\textit{S}$ in the given array $\textit{nums}$, and then calculate the number of ways for each subsequence $\textit{T}$ such that the sum of $\textit{T}$ equals $\textit{k}$. + +We define $f[i][j]$ to represent the number of ways to form subsequences with the first $i$ numbers such that the sum of each subsequence equals $j$. Initially, $f[0][0] = 1$, and all other positions are $0$. + +For the $i$-th number $x$, there are three cases: + +1. Not in the subsequence $\textit{S}$, in which case $f[i][j] = f[i-1][j]$; +2. In the subsequence $\textit{S}$, but not in the subsequence $\textit{T}$, in which case $f[i][j] = f[i-1][j]$; +3. In the subsequence $\textit{S}$, and in the subsequence $\textit{T}$, in which case $f[i][j] = f[i-1][j-x]$. + +In summary, the state transition equation is: + +$$ +f[i][j] = f[i-1][j] \times 2 + f[i-1][j-x] +$$ + +The final answer is $f[n][k]$. + +The time complexity is $O(n \times k)$, and the space complexity is $O(n \times k)$. Here, $n$ is the length of the array $\textit{nums}$, and $k$ is the given positive integer. #### Python3 ```python - +class Solution: + def sumOfPower(self, nums: List[int], k: int) -> int: + mod = 10**9 + 7 + n = len(nums) + f = [[0] * (k + 1) for _ in range(n + 1)] + f[0][0] = 1 + for i, x in enumerate(nums, 1): + for j in range(k + 1): + f[i][j] = f[i - 1][j] * 2 % mod + if j >= x: + f[i][j] = (f[i][j] + f[i - 1][j - x]) % mod + return f[n][k] ``` #### Java ```java - +class Solution { + public int sumOfPower(int[] nums, int k) { + final int mod = (int) 1e9 + 7; + int n = nums.length; + int[][] f = new int[n + 1][k + 1]; + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + for (int j = 0; j <= k; ++j) { + f[i][j] = (f[i - 1][j] * 2) % mod; + if (j >= nums[i - 1]) { + f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod; + } + } + } + return f[n][k]; + } +} ``` #### C++ ```cpp +class Solution { +public: + int sumOfPower(vector& nums, int k) { + const int mod = 1e9 + 7; + int n = nums.size(); + int f[n + 1][k + 1]; + memset(f, 0, sizeof(f)); + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + for (int j = 0; j <= k; ++j) { + f[i][j] = (f[i - 1][j] * 2) % mod; + if (j >= nums[i - 1]) { + f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod; + } + } + } + return f[n][k]; + } +}; +``` +#### Go + +```go +func sumOfPower(nums []int, k int) int { + const mod int = 1e9 + 7 + n := len(nums) + f := make([][]int, n+1) + for i := range f { + f[i] = make([]int, k+1) + } + f[0][0] = 1 + for i := 1; i <= n; i++ { + for j := 0; j <= k; j++ { + f[i][j] = (f[i-1][j] * 2) % mod + if j >= nums[i-1] { + f[i][j] = (f[i][j] + f[i-1][j-nums[i-1]]) % mod + } + } + } + return f[n][k] +} +``` + +#### TypeScript + +```ts +function sumOfPower(nums: number[], k: number): number { + const mod = 10 ** 9 + 7; + const n = nums.length; + const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0)); + f[0][0] = 1; + for (let i = 1; i <= n; ++i) { + for (let j = 0; j <= k; ++j) { + f[i][j] = (f[i - 1][j] * 2) % mod; + if (j >= nums[i - 1]) { + f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod; + } + } + } + return f[n][k]; +} +``` + + + + + + + +### Solution 2: Dynamic Programming (Optimization) + +In the state transition equation from Solution 1, the value of $f[i][j]$ only depends on $f[i-1][j]$ and $f[i-1][j-x]$. Therefore, we can optimize the first dimension of the space, reducing the space complexity to $O(k)$. + +Time complexity is $O(n \times k)$, and space complexity is $O(k)$. Here, $n$ is the length of the array $\textit{nums}$, and $k$ is the given positive integer. + + + +#### Python3 + +```python +class Solution: + def sumOfPower(self, nums: List[int], k: int) -> int: + mod = 10**9 + 7 + f = [1] + [0] * k + for x in nums: + for j in range(k, -1, -1): + f[j] = (f[j] * 2 + (0 if j < x else f[j - x])) % mod + return f[k] +``` + +#### Java + +```java +class Solution { + public int sumOfPower(int[] nums, int k) { + final int mod = (int) 1e9 + 7; + int[] f = new int[k + 1]; + f[0] = 1; + for (int x : nums) { + for (int j = k; j >= 0; --j) { + f[j] = (f[j] * 2 % mod + (j >= x ? f[j - x] : 0)) % mod; + } + } + return f[k]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int sumOfPower(vector& nums, int k) { + const int mod = 1e9 + 7; + int f[k + 1]; + memset(f, 0, sizeof(f)); + f[0] = 1; + for (int x : nums) { + for (int j = k; j >= 0; --j) { + f[j] = (f[j] * 2 % mod + (j >= x ? f[j - x] : 0)) % mod; + } + } + return f[k]; + } +}; ``` #### Go ```go +func sumOfPower(nums []int, k int) int { + const mod int = 1e9 + 7 + f := make([]int, k+1) + f[0] = 1 + for _, x := range nums { + for j := k; j >= 0; j-- { + f[j] = f[j] * 2 % mod + if j >= x { + f[j] = (f[j] + f[j-x]) % mod + } + } + } + return f[k] +} +``` +#### TypeScript + +```ts +function sumOfPower(nums: number[], k: number): number { + const mod = 10 ** 9 + 7; + const f: number[] = Array(k + 1).fill(0); + f[0] = 1; + for (const x of nums) { + for (let j = k; ~j; --j) { + f[j] = (f[j] * 2) % mod; + if (j >= x) { + f[j] = (f[j] + f[j - x]) % mod; + } + } + } + return f[k]; +} ``` diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.cpp b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.cpp new file mode 100644 index 0000000000000..2387620380a7d --- /dev/null +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int sumOfPower(vector& nums, int k) { + const int mod = 1e9 + 7; + int n = nums.size(); + int f[n + 1][k + 1]; + memset(f, 0, sizeof(f)); + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + for (int j = 0; j <= k; ++j) { + f[i][j] = (f[i - 1][j] * 2) % mod; + if (j >= nums[i - 1]) { + f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod; + } + } + } + return f[n][k]; + } +}; diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.go b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.go new file mode 100644 index 0000000000000..dfb0854e3bd09 --- /dev/null +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.go @@ -0,0 +1,18 @@ +func sumOfPower(nums []int, k int) int { + const mod int = 1e9 + 7 + n := len(nums) + f := make([][]int, n+1) + for i := range f { + f[i] = make([]int, k+1) + } + f[0][0] = 1 + for i := 1; i <= n; i++ { + for j := 0; j <= k; j++ { + f[i][j] = (f[i-1][j] * 2) % mod + if j >= nums[i-1] { + f[i][j] = (f[i][j] + f[i-1][j-nums[i-1]]) % mod + } + } + } + return f[n][k] +} diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.java b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.java new file mode 100644 index 0000000000000..34bb4f773274c --- /dev/null +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.java @@ -0,0 +1,17 @@ +class Solution { + public int sumOfPower(int[] nums, int k) { + final int mod = (int) 1e9 + 7; + int n = nums.length; + int[][] f = new int[n + 1][k + 1]; + f[0][0] = 1; + for (int i = 1; i <= n; ++i) { + for (int j = 0; j <= k; ++j) { + f[i][j] = (f[i - 1][j] * 2) % mod; + if (j >= nums[i - 1]) { + f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod; + } + } + } + return f[n][k]; + } +} diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.py b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.py new file mode 100644 index 0000000000000..51280aba941ee --- /dev/null +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def sumOfPower(self, nums: List[int], k: int) -> int: + mod = 10**9 + 7 + n = len(nums) + f = [[0] * (k + 1) for _ in range(n + 1)] + f[0][0] = 1 + for i, x in enumerate(nums, 1): + for j in range(k + 1): + f[i][j] = f[i - 1][j] * 2 % mod + if j >= x: + f[i][j] = (f[i][j] + f[i - 1][j - x]) % mod + return f[n][k] diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.ts b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.ts new file mode 100644 index 0000000000000..188f71ed27019 --- /dev/null +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution.ts @@ -0,0 +1,15 @@ +function sumOfPower(nums: number[], k: number): number { + const mod = 10 ** 9 + 7; + const n = nums.length; + const f: number[][] = Array.from({ length: n + 1 }, () => Array(k + 1).fill(0)); + f[0][0] = 1; + for (let i = 1; i <= n; ++i) { + for (let j = 0; j <= k; ++j) { + f[i][j] = (f[i - 1][j] * 2) % mod; + if (j >= nums[i - 1]) { + f[i][j] = (f[i][j] + f[i - 1][j - nums[i - 1]]) % mod; + } + } + } + return f[n][k]; +} diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.cpp b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.cpp new file mode 100644 index 0000000000000..abe9d96057e65 --- /dev/null +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.cpp @@ -0,0 +1,15 @@ +class Solution { +public: + int sumOfPower(vector& nums, int k) { + const int mod = 1e9 + 7; + int f[k + 1]; + memset(f, 0, sizeof(f)); + f[0] = 1; + for (int x : nums) { + for (int j = k; j >= 0; --j) { + f[j] = (f[j] * 2 % mod + (j >= x ? f[j - x] : 0)) % mod; + } + } + return f[k]; + } +}; diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.go b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.go new file mode 100644 index 0000000000000..d93a8a02d7b2f --- /dev/null +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.go @@ -0,0 +1,14 @@ +func sumOfPower(nums []int, k int) int { + const mod int = 1e9 + 7 + f := make([]int, k+1) + f[0] = 1 + for _, x := range nums { + for j := k; j >= 0; j-- { + f[j] = f[j] * 2 % mod + if j >= x { + f[j] = (f[j] + f[j-x]) % mod + } + } + } + return f[k] +} diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.java b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.java new file mode 100644 index 0000000000000..89ff2cf9c3664 --- /dev/null +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.java @@ -0,0 +1,13 @@ +class Solution { + public int sumOfPower(int[] nums, int k) { + final int mod = (int) 1e9 + 7; + int[] f = new int[k + 1]; + f[0] = 1; + for (int x : nums) { + for (int j = k; j >= 0; --j) { + f[j] = (f[j] * 2 % mod + (j >= x ? f[j - x] : 0)) % mod; + } + } + return f[k]; + } +} diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.py b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.py new file mode 100644 index 0000000000000..403c3d9ba2475 --- /dev/null +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.py @@ -0,0 +1,8 @@ +class Solution: + def sumOfPower(self, nums: List[int], k: int) -> int: + mod = 10**9 + 7 + f = [1] + [0] * k + for x in nums: + for j in range(k, -1, -1): + f[j] = (f[j] * 2 + (0 if j < x else f[j - x])) % mod + return f[k] diff --git a/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.ts b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.ts new file mode 100644 index 0000000000000..5fa89b2561f12 --- /dev/null +++ b/solution/3000-3099/3082.Find the Sum of the Power of All Subsequences/Solution2.ts @@ -0,0 +1,14 @@ +function sumOfPower(nums: number[], k: number): number { + const mod = 10 ** 9 + 7; + const f: number[] = Array(k + 1).fill(0); + f[0] = 1; + for (const x of nums) { + for (let j = k; ~j; --j) { + f[j] = (f[j] * 2) % mod; + if (j >= x) { + f[j] = (f[j] + f[j - x]) % mod; + } + } + } + return f[k]; +}