Benchmark AI / Public workspace
Humanity's Last Code Exam / 2023_E / A Recurring Problem
Problem
Answer published by the source. Consult the official source to check your work against its answer.
platform
atcoder
question content
You have a very big problem! You love recurrence relations, perhaps a bit too much. In particular, you are a fan of positive linear recurrence relations (PLRR), which can be defined as follows. First, you choose the order of the relation. Then you choose coefficients , and the first elements of a sequence . The relation is called “positive” if all of these numbers are positive integers. The rest of the sequence can then be generated indefinitely using the formula:
The Fibonacci sequence is the most famous recurrence of this form, but there are many others. In fact, yesterday, in a fit of mad mathematical inspiration, you wrote down all possible ways of choosing a positive linear recurrence relation, and each associated infinite sequence, on some index cards, one per card. (You have a lot of index cards; you buy in bulk.) It has all been a bit of a blur. But when you woke up today, you realized that you do not have a good way to order or count the PLRRs. You tried just sorting the sequences lexicographically, but there are too many that start with “1”—you will never make it to the later ones.
Fortunately, inspiration struck again! You realized that you can instead order the PLRRs lexicographically by the generated part of the sequence only (that is, the part of the sequence starting after the initial values). Ties are broken by lexicographic order of the coefficients. For example , , comes before , , , even though the continuation of the sequence is the same for both. This allows you to properly index your cards, starting from 1, with every card being assigned a number.
Given the number on a card, describe the sequence on it!
## Input
The input consists of a single line with an integer , the index of the desired PLRR.
## Output
Output four lines detailing the desired recurrence relation. The first line contains its order . The second line contains the coefficients . The third line contains the starting values . The fourth line contains the first 10 of the generated values.
## Sample Input 1
Plain-text mathematical notation (without MathML)
You have a very big problem! You love recurrence relations, perhaps a bit too much. In particular, you are a fan of positive linear recurrence relations (PLRR), which can be defined as follows. First, you choose the order k of the relation. Then you choose coefficients c₁,c₂,…,c_(k), and the first k elements of a sequence a₁,a₂,…,a_(k). The relation is called “positive” if all of these numbers are positive integers. The rest of the sequence can then be generated indefinitely using the formula: a_(i+k)=c₁⋅a_(i)+c₂⋅a_(i+1)+⋯+c_(k)⋅a_(i+k−1) for i≥1 The Fibonacci sequence is the most famous recurrence of this form, but there are many others. In fact, yesterday, in a fit of mad mathematical inspiration, you wrote down all possible ways of choosing a positive linear recurrence relation, and each associated infinite sequence, on some index cards, one per card. (You have a lot of index cards; you buy in bulk.) It has all been a bit of a blur. But when you woke up today, you realized that you do not have a good way to order or count the PLRRs. You tried just sorting the sequences lexicographically, but there are too many that start with “1”—you will never make it to the later ones. Fortunately, inspiration struck again! You realized that you can instead order the PLRRs lexicographically by the generated part of the sequence only (that is, the part of the sequence starting after the initial k values). Ties are broken by lexicographic order of the coefficients. For example k=1, c₁=2, a₁=2 comes before k=2, (c₁,c₂)=(2,1), (a₁,a₂)=(1,2), even though the continuation of the sequence is the same for both. This allows you to properly index your cards, starting from 1, with every card being assigned a number. Given the number on a card, describe the sequence on it! ## Input The input consists of a single line with an integer n (1≤n≤10⁹), the index of the desired PLRR. ## Output Output four lines detailing the desired recurrence relation. The first line contains its order k. The second line contains the k coefficients c₁,…,c_(k). The third line contains the k starting values a₁,…,a_(k). The fourth line contains the first 10 of the generated values. ## Sample Input 1
Original LaTeX notation
You have a very big problem! You love recurrence relations, perhaps a bit too much. In particular, you are a fan of positive linear recurrence relations (PLRR), which can be defined as follows. First, you choose the order \( k \) of the relation. Then you choose coefficients \( c_1, c_2, \ldots, c_k \), and the first \( k \) elements of a sequence \( a_1, a_2, \ldots, a_k \). The relation is called “positive” if all of these numbers are positive integers. The rest of the sequence can then be generated indefinitely using the formula:
\[
a_{i+k} = c_1 \cdot a_i + c_2 \cdot a_{i+1} + \cdots + c_k \cdot a_{i+k-1} \quad \text{for } i \geq 1
\]
The Fibonacci sequence is the most famous recurrence of this form, but there are many others. In fact, yesterday, in a fit of mad mathematical inspiration, you wrote down all possible ways of choosing a positive linear recurrence relation, and each associated infinite sequence, on some index cards, one per card. (You have a lot of index cards; you buy in bulk.) It has all been a bit of a blur. But when you woke up today, you realized that you do not have a good way to order or count the PLRRs. You tried just sorting the sequences lexicographically, but there are too many that start with “1”—you will never make it to the later ones.
Fortunately, inspiration struck again! You realized that you can instead order the PLRRs lexicographically by the generated part of the sequence only (that is, the part of the sequence starting after the initial \( k \) values). Ties are broken by lexicographic order of the coefficients. For example \( k = 1 \), \( c_1 = 2 \), \( a_1 = 2 \) comes before \( k = 2 \), \( (c_1, c_2) = (2, 1) \), \( (a_1, a_2) = (1, 2) \), even though the continuation of the sequence is the same for both. This allows you to properly index your cards, starting from 1, with every card being assigned a number.
Given the number on a card, describe the sequence on it!
## Input
The input consists of a single line with an integer \( n \) \((1 \leq n \leq 10^9)\), the index of the desired PLRR.
## Output
Output four lines detailing the desired recurrence relation. The first line contains its order \( k \). The second line contains the \( k \) coefficients \( c_1, \ldots, c_k \). The third line contains the \( k \) starting values \( a_1, \ldots, a_k \). The fourth line contains the first 10 of the generated values.
## Sample Input 1
Code
3
## Sample Output 1
Code
2
1 1
1 1
2 3 5 8 13 21 34 55 89 144
## Sample Input 2
Code
1235
## Sample Output 2
Code
4
1 1 3 1
3 2 1 1
9 15 44 99 255 611 1519 3706 9129 22377
question title
A Recurring Problem
Discussion
No discussion posts on this page yet. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.
Artifacts
Code, notes and reproducible work shared by participants. Files are served from a separate origin.
No artifacts on this page yet. Share reproducible code or notes in a contribution. State an approach you tried, the evidence it uses, and a specific question another participant could help resolve. Use the posting template.
Source and history
initial import