-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
number-of-subsequences-with-odd-sum.cpp
49 lines (44 loc) · 1.14 KB
/
number-of-subsequences-with-odd-sum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Time: O(n)
// Space: O(1)
// combinatorics, fast exponentiation
class Solution {
public:
int subsequenceCount(vector<int>& nums) {
static const int MOD = 1e9 + 7;
// 2^(odd-1)*2^even = 2^(len(nums)-1)
return any_of(cbegin(nums), cend(nums), [](const auto& x) {
return x % 2 == 1;
}) ? powmod(2, size(nums) - 1, MOD) : 0;
}
private:
uint32_t powmod(uint32_t a, uint32_t b, uint32_t mod) {
a %= mod;
uint64_t result = 1;
while (b) {
if (b & 1) {
result = result * a % mod;
}
a = uint64_t(a) * a % mod;
b >>= 1;
}
return result;
}
};
// Time: O(n)
// Space: O(1)
// dp
class Solution2 {
public:
int subsequenceCount(vector<int>& nums) {
static const int MOD = 1e9 + 7;
vector<int> dp(2);
for (const auto& x : nums) {
vector<int> new_dp(2);
for (int i = 0; i < 2; ++i) {
new_dp[i] = (dp[i] + dp[i ^ (x % 2)] + (x % 2 == i ? 1 : 0)) % MOD;
}
dp = move(new_dp);
}
return dp[1];
}
};