solve the question in C++

Tạo vào: 5 tháng 7, 2025

Trả lời bằng O3 bởi Chat01

Câu hỏi

69a76e25-2420-40a2-b709-01c4ad871b3c.jpg69a76e25-2420-40a2-b709-01c4ad871b3c.jpg
ffd871ae-18db-4131-9d68-4fc348d21906.jpgffd871ae-18db-4131-9d68-4fc348d21906.jpg
091da2a9-8567-49c9-814c-650889234628.jpg091da2a9-8567-49c9-814c-650889234628.jpg
430bf362-4a40-409b-aba6-47d8ac18fe13.jpg430bf362-4a40-409b-aba6-47d8ac18fe13.jpg
794f64b5-2d31-447e-a2bd-26a5d4c58c95.jpg794f64b5-2d31-447e-a2bd-26a5d4c58c95.jpg
537bf14f-bf38-4564-a0a1-a82e3597cfa1 (1).jpg537bf14f-bf38-4564-a0a1-a82e3597cfa1 (1).jpg

solve the question in C++

Python

python
def count_subseq(arr): MOD=10**9+7 dpE1=dpE2=dpO1=dpO2=0 for x in arr: if x%2==0: nE1=(dpO1+dpO2+1)%MOD nE2=dpE1%MOD dpE1=(dpE1+nE1)%MOD dpE2=(dpE2+nE2)%MOD else: nO1=(dpE1+dpE2+1)%MOD nO2=dpO1%MOD dpO1=(dpO1+nO1)%MOD dpO2=(dpO2+nO2)%MOD return (dpE1+dpE2+dpO1+dpO2)%MOD print(count_subseq([1,2,3,5]))

Python

python
print(count_subseq([2,2,2]))

Suy nghĩ

Updating totals and checks

I need to make sure I properly calculate the new totals after each step. For even subsequences, I add the new subsequences created by the current element (nE1), ensuring there's no overlap with existing subsequences (dpE1). Similarly, I update dpE2 and keep odd subsequences unchanged. When I switch to odd calculations, I adjust for sequences ending in odd length. At the end, the total non-empty subsequences is the sum of all four states. I’ll verify this logic using a test case.

Reasoning through subsequences

Okay, I'm considering how subsequences are counted in the algorithm. For each element, there are subsequences that either include or exclude it. The counts for those including the element should be updated accordingly. So, for even subsequences, I'll update dpE1 and dp

Thought for 43 seconds

Trả lời

Idea & Approach

Only the parity of every element matters, because the rule is about consecutive evens or odds.
While we scan the array we maintain how many valid subsequences end with the last 1 or 2 elements being even/odd:

statemeaning
E1ends in one even (… odd , even)
E2ends in two evens (… odd , even, even)
O1ends in one odd (… even , odd)
O2ends in two odds (… even , odd, odd)

A third same-parity element would violate the rule, so we never create such a state.

Processing an element x:

If x is even

text
newE1 = 1 // subseq consisting of only x + O1 + O2 // append x after an odd run newE2 = E1 // append x after a single even // sequences that ignore x keep their old states E1 = (E1 + newE1) mod M E2 = (E2 + newE2) mod M

If x is odd – symmetric with the roles of even/odd swapped.

At each step we keep everything mod M = 1 000 000 007.
After the whole array the answer is

(E1 + E2 + O1 + O2) mod M

because every non-empty valid subsequence ends in exactly one of the four states.

The algorithm is O(N) per test case and O(1) extra memory.


Reference implementation (C++17)

cpp
#include <bits/stdc++.h> using namespace std; const long long MOD = 1'000'000'007LL; int solve(int N, const vector<int>& A) { long long E1 = 0, E2 = 0, O1 = 0, O2 = 0; for (int x : A) { if (x & 1) { // x is odd long long newO1 = (1 + E1 + E2) % MOD; long long newO2 = E1 ? 0 : 0; // will be set below newO2 = O1; // append odd after single odd O1 = (O1 + newO1) % MOD; O2 = (O2 + newO2) % MOD; } else { // x is even long long newE1 = (1 + O1 + O2) % MOD; long long newE2 = E1; // append even after single even E1 = (E1 + newE1) % MOD; E2 = (E2 + newE2) % MOD; } } return int((E1 + E2 + O1 + O2) % MOD); } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while (T--) { int N; cin >> N; vector<int> A(N); for (int &x : A) cin >> x; cout << solve(N, A) << '\n'; } return 0; }

Why it works

  • Every subsequence is built exactly once (the moment we read its last element).
  • No subsequence with a run of 3 same-parity numbers is ever generated, because we never allow a transition from E2 → E3 or O2 → O3.
  • Modulo arithmetic keeps numbers in range.

This passes the sample cases ([2,2,2] → 6, [1,2,3,5] → 14) and runs easily within the given bounds (N ≤ 10^5, T ≤ 10).

Chia sẻ Q&A này