Skip to content

Commit 6b74c45

Browse files
authored
Added task 3757
1 parent bd16cf5 commit 6b74c45

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package g3701_3800.s3757_number_of_effective_subsequences;
2+
3+
// #Hard #Array #Dynamic_Programming #Math #Bit_Manipulation #Combinatorics #Weekly_Contest_477
4+
// #Senior_Staff #2026_04_25_Time_128_ms_(95.71%)_Space_85.91_MB_(18.57%)
5+
6+
public class Solution {
7+
private static final int MOD = 1_000_000_007;
8+
9+
public int countEffective(int[] nums) {
10+
int n = nums.length;
11+
int t = 0;
12+
for (int v : nums) {
13+
t |= v;
14+
}
15+
if (t == 0) {
16+
return 0;
17+
}
18+
int[] bits = new int[20];
19+
int m = 0;
20+
for (int b = 0; b < 20; ++b) {
21+
if (((t >> b) & 1) != 0) {
22+
bits[m++] = b;
23+
}
24+
}
25+
int s = 1 << m;
26+
int[] freq = new int[s];
27+
for (int v : nums) {
28+
int m1 = 0;
29+
for (int j = 0; j < m; ++j) {
30+
if (((v >> bits[j]) & 1) != 0) {
31+
m1 |= 1 << j;
32+
}
33+
}
34+
freq[m1]++;
35+
}
36+
int[] f = new int[s];
37+
System.arraycopy(freq, 0, f, 0, s);
38+
for (int i = 0; i < m; ++i) {
39+
for (int mask = 0; mask < s; ++mask) {
40+
if ((mask & (1 << i)) != 0) {
41+
f[mask] += f[mask ^ (1 << i)];
42+
}
43+
}
44+
}
45+
long[] p2 = new long[n + 1];
46+
p2[0] = 1;
47+
for (int i = 1; i <= n; ++i) {
48+
p2[i] = (p2[i - 1] << 1) % MOD;
49+
}
50+
long ans = 0;
51+
int all = s - 1;
52+
for (int bmask = 1; bmask < s; ++bmask) {
53+
int comp = all ^ bmask;
54+
int cnt = f[comp];
55+
long add = p2[cnt];
56+
if (Integer.bitCount(bmask) % 2 == 1) {
57+
ans = (ans + add) % MOD;
58+
} else {
59+
ans = (ans - add) % MOD;
60+
}
61+
}
62+
ans = (ans % MOD + MOD) % MOD;
63+
return (int) ans;
64+
}
65+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
3757\. Number of Effective Subsequences
2+
3+
Hard
4+
5+
You are given an integer array `nums`.
6+
7+
The **strength** of the array is defined as the **bitwise OR** of all its elements.
8+
9+
A **subsequence** is considered **effective** if removing that subsequence **strictly decreases** the strength of the remaining elements.
10+
11+
Return the number of **effective subsequences** in `nums`. Since the answer may be large, return it **modulo** <code>10<sup>9</sup> + 7</code>.
12+
13+
The bitwise OR of an empty array is 0.
14+
15+
**Example 1:**
16+
17+
**Input:** nums = [1,2,3]
18+
19+
**Output:** 3
20+
21+
**Explanation:**
22+
23+
* The Bitwise OR of the array is `1 OR 2 OR 3 = 3`.
24+
* Subsequences that are effective are:
25+
* `[1, 3]`: The remaining element `[2]` has a Bitwise OR of 2.
26+
* `[2, 3]`: The remaining element `[1]` has a Bitwise OR of 1.
27+
* `[1, 2, 3]`: The remaining elements `[]` have a Bitwise OR of 0.
28+
* Thus, the total number of effective subsequences is 3.
29+
30+
**Example 2:**
31+
32+
**Input:** nums = [7,4,6]
33+
34+
**Output:** 4
35+
36+
**Explanation:**
37+
38+
* The Bitwise OR of the array is `7 OR 4 OR 6 = 7`.
39+
* Subsequences that are effective are:
40+
* `[7]`: The remaining elements `[4, 6]` have a Bitwise OR of 6.
41+
* `[7, 4]`: The remaining element `[6]` has a Bitwise OR of 6.
42+
* `[7, 6]`: The remaining element `[4]` has a Bitwise OR of 4.
43+
* `[7, 4, 6]`: The remaining elements `[]` have a Bitwise OR of 0.
44+
* Thus, the total number of effective subsequences is 4.
45+
46+
**Example 3:**
47+
48+
**Input:** nums = [8,8]
49+
50+
**Output:** 1
51+
52+
**Explanation:**
53+
54+
* The Bitwise OR of the array is `8 OR 8 = 8`.
55+
* Only the subsequence `[8, 8]` is effective since removing it leaves `[]` which has a Bitwise OR of 0.
56+
* Thus, the total number of effective subsequences is 1.
57+
58+
**Example 4:**
59+
60+
**Input:** nums = [2,2,1]
61+
62+
**Output:** 5
63+
64+
**Explanation:**
65+
66+
* The Bitwise OR of the array is `2 OR 2 OR 1 = 3`.
67+
* Subsequences that are effective are:
68+
* `[1]`: The remaining elements `[2, 2]` have a Bitwise OR of 2.
69+
* `[2, 1]` (using `nums[0]`, `nums[2]`): The remaining element `[2]` has a Bitwise OR of 2.
70+
* `[2, 1]` (using `nums[1]`, `nums[2]`): The remaining element `[2]` has a Bitwise OR of 2.
71+
* `[2, 2]`: The remaining element `[1]` has a Bitwise OR of 1.
72+
* `[2, 2, 1]`: The remaining elements `[]` have a Bitwise OR of 0.
73+
* Thus, the total number of effective subsequences is 5.
74+
75+
**Constraints:**
76+
77+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
78+
* <code>1 <= nums[i] <= 10<sup>6</sup></code>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g3701_3800.s3757_number_of_effective_subsequences;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void countEffective() {
11+
assertThat(new Solution().countEffective(new int[] {1, 2, 3}), equalTo(3));
12+
}
13+
14+
@Test
15+
void countEffective2() {
16+
assertThat(new Solution().countEffective(new int[] {7, 4, 6}), equalTo(4));
17+
}
18+
19+
@Test
20+
void countEffective3() {
21+
assertThat(new Solution().countEffective(new int[] {8, 8}), equalTo(1));
22+
}
23+
24+
@Test
25+
void countEffective4() {
26+
assertThat(new Solution().countEffective(new int[] {2, 2, 1}), equalTo(5));
27+
}
28+
}

0 commit comments

Comments
 (0)