D. Fibonacci Words
Problem ID: fibonacci The Fibonacci word sequence of bit strings is defined as: if n = 0 0 F (n) = 1 if n = 1 F (n − 1) + F (n − 2) if n ≥ 2 Here + denotes concatenation of strings. The first few elements are: n F (n) 0 0...
Problem Statement
Formatted from the contest statement text, with sample tests broken out into copyable blocks.
The Fibonacci word sequence of bit strings is defined as:
if n = 0
0
F (n) = 1 if n = 1
F (n − 1) + F (n − 2) if n ≥ 2
Here + denotes concatenation of strings. The first few elements are:
n F (n)
0 0
1 1
2 10
3 101
4 10110
5 10110101
6 1011010110110
7 101101011011010110101
8 1011010110110101101011011010110110
9 1011010110110101101011011010110110101101011011010110101
Given a bit pattern p and a number n, how often does p occur in F (n)?Input
The first line of each test case contains the integer n (0 ≤ n ≤ 100). The second line contains the bit pattern p. The pattern p is nonempty and has a length of at most 100 000 characters.
Output
For each test case, display its case number followed by the number of occurrences of the bit pattern p in F (n). Occurrences may overlap. The number of occurrences will be less than 263 .
Sample Input Output for Sample Input
6 Case 1: 5
10 Case 2: 8
7 Case 3: 4
10 Case 4: 4
6 Case 5: 7540113804746346428
01
6
101
96
10110101101101
8Editorial
The solution write-up is rendered from the LaTeX source, with equations kept live through MathJax.
Key Observations
Write the structural observations that make the problem tractable.
State any useful invariant, monotonicity property, graph interpretation, or combinatorial reformulation.
If the constraints matter, explain exactly which part of the solution they enable.
Algorithm
Describe the data structures and the state maintained by the algorithm.
Explain the processing order and why it is sufficient.
Mention corner cases explicitly if they affect the implementation.
Correctness Proof
We prove that the algorithm returns the correct answer.
Lemma 1.
State the first key claim.
Proof.
Provide a concise proof.
Lemma 2.
State the next claim if needed.
Proof.
Provide a concise proof.
Theorem.
The algorithm outputs the correct answer for every valid input.
Proof.
Combine the lemmas and finish the argument.
Complexity Analysis
State the running time and memory usage in terms of the input size.
Implementation Notes
Mention any non-obvious implementation detail that is easy to get wrong.
Mention numeric limits, indexing conventions, or tie-breaking rules if relevant.
Code
C++ solution used for this page.
#include <bits/stdc++.h>
using namespace std;
namespace {
void solve() {
// Fill in the full solution logic for the problem here.
}
} // namespace
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solve();
return 0;
}
Source Files and Assets
Raw files are still available here when you want the original TeX, C++, or statement assets.
Show raw files
competitive_programming/icpc/2012/D-fibonacci-words/solution.texC++ implementationcompetitive_programming/icpc/2012/D-fibonacci-words/solution.cppStatement textcompetitive_programming/icpc/2012/D-fibonacci-words/statement.txtStatement PDFcompetitive_programming/icpc/2012/D-fibonacci-words/statement.pdfMetadatacompetitive_programming/icpc/2012/D-fibonacci-words/meta.jsonYear packetcompetitive_programming/icpc/2012/contest_problems.pdf