in cpp Count subsequences You are given an array A...
Created on: July 5, 2025
Created on: July 5, 2025
in cpp
Count subsequences
You are given an array A of N integer elements.
Task
Determine the number of the non-empty subsequences of array A such that there do not exist three consecutive even or three consecutive odd integers in the subsequence. As the answer can be large, print it modulo 109 + 7.
Notes
Assume 1-based indexing.
A subsequence of array A can be obtained by deleting some array elements while keeping the order of other elements intact
Below is an O(N) time / O(1) memory dynamic-programming solution in modern C++.
The key idea is to remember, for every valid subsequence we have built so far, only the parity of its last two selected elements matters:
State | Meaning |
---|---|
e1 | subsequences that end with one even in a row (…E) |
e2 | subsequences that end with two evens in a row (…EE) |
o1 | subsequences that end with one odd in a row (…O) |
o2 | subsequences that end with two odds in a row (…OO) |
When we scan the next element x
:
x
.x
to every subsequence whose current tail parity differs – the run length resets to 1.x
to every subsequence whose tail parity equals x
’s parity and whose run-length is 1 – the run length becomes 2.x
simply stay in their old state.Because we never allow a run length to reach 3, all generated subsequences are valid.
cpp#include <bits/stdc++.h> using namespace std; constexpr long long MOD = 1'000'000'007LL; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int N; if (!(cin >> N)) return 0; vector<long long> A(N); for (auto &v : A) cin >> v; long long e1 = 0, e2 = 0, o1 = 0, o2 = 0; // DP states for (long long x : A) { if ((x & 1) == 0) { // x is even long long add_e1 = (1 + o1 + o2) % MOD; // new subseq + extend odd-ending ones long long add_e2 = e1; // extend …E to …EE e1 = (e1 + add_e1) % MOD; e2 = (e2 + add_e2) % MOD; } else { // x is odd long long add_o1 = (1 + e1 + e2) % MOD; // new subseq + extend even-ending ones long long add_o2 = o1; // extend …O to …OO o1 = (o1 + add_o1) % MOD; o2 = (o2 + add_o2) % MOD; } } long long ans = (e1 + e2 + o1 + o2) % MOD; // all non-empty valid subsequences cout << ans << '\n'; return 0; }
O(N)
O(1)
(just four integers)Prints the answer modulo 1 000 000 007 as required.
int solve(int N, vector<int> A)
write the code in this function only
cppint solve(int N, std::vector<int> A) { constexpr long long MOD = 1'000'000'007LL; long long e1 = 0, e2 = 0, o1 = 0, o2 = 0; // DP buckets for (int x : A) { if ((x & 1) == 0) { // even long long add_e1 = (1 + o1 + o2) % MOD; // start new or extend odd-ending long long add_e2 = e1; // extend …E to …EE e1 = (e1 + add_e1) % MOD; e2 = (e2 + add_e2) % MOD; } else { // odd long long add_o1 = (1 + e1 + e2) % MOD; // start new or extend even-ending long long add_o2 = o1; // extend …O to …OO o1 = (o1 + add_o1) % MOD; o2 = (o2 + add_o2) % MOD; } } return static_cast<int>((e1 + e2 + o1 + o2) % MOD); // all non-empty valid subsequences }