Find the number of subsequences of size n, with sum such that after taking modulo m, the sum became greater or equal to x.
Where:
1 <= n <= 42
1 <= m <= 10^9
0 <= x < m
How to solve this?
Asked
Active
Viewed 74 times
-1

user13313695
- 7
- 2
-
1Try forming all combinations of the first 21 elements, and all combinations of the second 21 elements, and working out how to combine these to get the answer. – Peter de Rivaz Nov 03 '22 at 21:43
-
How big can the input array be? – Dave Nov 03 '22 at 21:58
-
@Dave Maximum size of the array is 42 – user13313695 Nov 04 '22 at 04:07
-
@user13313695 Ok, so where your question says the subsequence is of size n, it should say the input array is of size n, correct? And then the subsequence can be of any size <=n? – Dave Nov 04 '22 at 12:24
1 Answers
1
This problem is easier to solve with a search technique called "Meet in the middle". As Peter de Rivaz suggests you can split up your input into two sets A and B, each with 21 or less elements.
Step 1:
There are 2^|A| combinations of elements in A, and for each of these calculate the sum mod m
and store this in a table T
. Sort the table T
.
Step 2:
For each combination c
of the 2^|B| combinations of elements of B, calculate the sum s
of c mod m
. Now you can determine the range i->j s.t. s + i >= x mod m
, using binary search on T
you can determine the number of table entries of T that are in the required range.
The running time of this is O(2^{n/2} * n) which for n = 42 is computable.

Christian Sloper
- 7,440
- 3
- 15
- 28