Problem · Dynamic Programming
Count Valid A-B-C Sequences Under a Modulo-Four Rule
Learn this problemProblem statement
Build a sequence of length n using the characters A, B, and C. Start with a total cost of zero.
- Placing
Aincreases the total cost by1. - Placing
BorCincreases the total cost by0. - If the current total cost is congruent to
3modulo4, you may not placeCnext.
A completed sequence is accepted when its final total cost is divisible by 4.
Return the number of accepted sequences of length n, modulo 1000000007.
Function
countAcceptedSequences(n: long) → intExamples
Example 1
n = 1return = 2The accepted sequences are B and C. The sequence A finishes with cost one.
Example 2
n = 4return = 17A four-state dynamic program for the current cost modulo four contains [17,32,24,7] sequences after four placements, so 17 finish in the accepted state.
Example 3
n = 10return = 9104Applying the same four-state transition ten times leaves 9104 sequences in residue state zero.
Constraints
1 <= n <= 1000000000000000000- Return the answer modulo
1000000007.
More infosys problems
- Maximum Product of a Strictly Increasing Contiguous SubarrayOA · Seen Aug 2026
- Minimum Cost to Assign Candidates to Two CitiesOA · Seen Aug 2026
- Minimum Path Sum With Grid SwitchesONSITE INTERVIEW · Seen May 2026
- Maximum Subarray Sum After SwapsOA · Seen Feb 2026
- Find Number of Good Subsequences 🍅OA · Seen Apr 2024
- Number of Unique Elements After Modifications 🍁OA · Seen Mar 2024
- Obtain Maximum Score Using Minimum SwapsOA · Seen Feb 2024
- Extract CardsOA · Seen Feb 2024