Prize Strings
A particular school offers cash prizes to children with good attendance and punctuality. If they are absent for three consecutive days or late on more than one day, their prize is forfeited. Over a...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
A particular school offers cash rewards to children with good attendance and punctuality. If they are absent for three consecutive days or late on more than one occasion then they forfeit their prize.
During an n-day period a trinary string is formed for each child consisting of L’s (late), O’s (on time), and A’s (absent).
Although there are eighty-one trinary strings for a 4-day period that can be formed, exactly forty-three strings would lead to a prize:
| OOOO | OOOA | OOOL | OOAO | OOAA | OOAL | OOLO | OOLA | OAOO | OAOA |
| OAOL | OAAO | OAAL | OALO | OALA | OLOO | OLOA | OLAO | OLAA | AOOO |
| AOOA | AOOL | AOAO | AOAA | AOAL | AOLO | AOLA | AAOO | AAOA | AAOL |
| AALO | AALA | ALOO | ALOA | ALAO | ALAA | LOOO | LOOA | LOAO | LOAA |
| LAOO | LAOA | LAAO |
How many "prize" strings exist over a 30-day period?
Problem 191: Prize Strings
Mathematical Development
Definition. Let and let denote the set of prize strings of length . Let denote the number of strings of length over that contain no three consecutive ‘s.
Theorem 1 (Tribonacci-type recurrence for ). The function satisfies
with initial values , , .
Proof. We partition the set of valid strings of length over (containing no ) by the length of the maximal suffix of ‘s. Every such string falls into exactly one of three disjoint cases:
- Suffix of zero ‘s (string ends in ): the prefix of length is an arbitrary valid string, giving strings.
- Suffix of exactly one (string ends in , or is itself when ): for , the character at position must be , and the prefix of length is an arbitrary valid string, giving strings.
- Suffix of exactly two ‘s (string ends in , or is when ): for , the character at position must be (since is forbidden), and the prefix of length is an arbitrary valid string, giving strings.
A suffix of three or more ‘s is impossible by the constraint. These three cases are exhaustive and mutually exclusive, establishing for .
The base cases are verified by direct enumeration:
- : the empty string .
- : the strings .
- : the strings .
Theorem 2 (Counting prize strings). The total number of prize strings of length is
Proof. We partition by the number of occurrences of .
Case 1 (zero ‘s). The string lies in with no substring. By definition, there are exactly such strings.
Case 2 (exactly one ). Fix the position of at index (0-indexed). The string decomposes as
The prefix is a string over of length containing no (counted by ). The suffix is a string over of length containing no (counted by ). Crucially, the at position interrupts any consecutive- run, so the prefix and suffix constraints are independent. By the multiplication principle, there are valid strings with at position . Summing over all gives .
Since a prize string has at most one , Cases 1 and 2 are exhaustive and disjoint.
Remark. The sum is a discrete convolution of with itself, evaluated at .
Corollary. For : and .
Editorial
Count 30-character strings over {O, A, L} with at most one L and no three consecutive A’s. We compute g[0..n] via the recurrence. Finally, accumulate |P_n|.
Pseudocode
Compute g[0..n] via the recurrence
Accumulate |P_n|
Complexity Analysis
- Time: arithmetic operations — for the recurrence and for the convolution sum. Each operation involves integers of size bits, so the bit-complexity is , though for all values fit in a single machine word.
- Space: to store the array . This can be reduced to auxiliary space by accumulating the convolution sum during the forward recurrence pass using a two-pointer technique, though the constant-factor savings are negligible for .
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
int main() {
// g[n] = number of valid strings of length n over {O,A} with no "AAA"
// Recurrence: g[n] = g[n-1] + g[n-2] + g[n-3]
const int N = 30;
vector<long long> g(N + 1);
g[0] = 1; g[1] = 2; g[2] = 4;
for (int i = 3; i <= N; i++)
g[i] = g[i-1] + g[i-2] + g[i-3];
// Strings with 0 L's
long long ans = g[N];
// Strings with exactly 1 L: place L at position k, 0-indexed
// prefix of length k and suffix of length N-1-k must both be valid no-L strings
for (int k = 0; k < N; k++)
ans += g[k] * g[N - 1 - k];
cout << ans << endl;
return 0;
}
"""
Problem 191: Prize Strings
Count 30-character strings over {O, A, L} with at most one L
and no three consecutive A's.
"""
def solve(n=30):
# g[i] = number of valid strings of length i with no L and no "AAA"
g = [0] * (n + 1)
g[0] = 1
g[1] = 2
g[2] = 4
for i in range(3, n + 1):
g[i] = g[i-1] + g[i-2] + g[i-3]
# No L: g[n]
ans = g[n]
# Exactly one L at position k: g[k] * g[n-1-k]
for k in range(n):
ans += g[k] * g[n - 1 - k]
return ans
answer = solve()
assert answer == 1918080160
print(answer)