CountBinarySequences
SRM 666 · 2015-06-30 · by praveen123
Problem Statement
Anu loves binary sequences of length n. A binary sequence of length n has n elements (indexed from 1 to n) and each of those elements is either a zero (0) or a one (1).
You are given the
The ranges defined by L and R satisfy one additional restriction: Given any two ranges, either one is contained in the other, or they are disjoint. For example, it cannot be the case that L = {1, 4, 10} and R = {5, 7, 20} because none of the ranges [1,5] and [4,7] contains the other, and they are also not disjoint.
Anu wants to count the number of binary sequences of length n that satisfy the following properties:
- There are never more than k consecutive ones in the sequence.
- Each of the ranges defined by L and R has an even sum.
Constraints
- n will be between 1 and 1,000,000,000 (10^9), inclusive.
- k will be between 1 and 5, inclusive.
- L will contain between 1 and 50 elements, inclusive.
- R will contain the same number of elements as L.
- For each valid i, L[i] will be less than or equal to R[i].
- All elements of L and R will be between 1 and n, inclusive.
- All ranges defined by L and R will be distinct.
- For any two ranges defined by L and R, either one will be contained in the other, or they will be disjoint.
4
2
{2}
{3}
Returns: 5
The five valid sequences are 0000, 0001, 0110, 1000, and 1001.
4
1
{2}
{2}
Returns: 6
We cannot have two consecutive ones. The range [2,2] contains only the index 2. As the sum of this range must be even, the element at index 2 must be a zero. Hence, the six valid sequences are 0000, 0001, 0010, 1000, 1001, and 1010.
6
3
{1, 2, 3}
{6, 5, 4}
Returns: 6
1000
4
{10, 101, 201, 110, 121}
{100, 200, 300, 120, 130}
Returns: 444743885
1
5
{1}
{1}
Returns: 1
Submissions are judged against all 143 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class CountBinarySequences with a public method int countSequences(int n, int k, vector<int> L, vector<int> R) · 143 test cases · 2 s / 256 MB per case