Editorial for TLE '16 Contest 8 P3 - Fool's Sequence
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
The first step to finding the desired number is to determine how long the number is. Obviously, a number with fewer digits is less than a number with more digits.
We can use dynamic programming to find the number of Fool's numbers with a certain length. Obviously, there are  Fool's numbers with length 
 or 
, and 
 Fool's number with length 
 or 
 each. With lengths greater than these, 
, since a Fool's number can be made by taking an existing Fool's number and appending 
 or 
. It is enough to calculate up to 
.
After using dynamic programming to find how long the desired Fool's number is, we now need to find the exact number, which happens to be the  Fool's number with the length we found. This gets rid of the Fool's numbers which are shorter than the correct length.
Suppose that the correct Fool's number currently has a prefix ; 
 can be empty. We note that 
 is always less than 
, if 
 is possible. If there are enough Fool's numbers starting with 
 (the exact count is 
 since there are 
 characters remaining), the correct prefix must also start with 
. Otherwise, the correct prefix would be 
. If this occurs, we must now remove all 
s from 
. Keep going until the exact Fool's number is found, that is, until 
.
Time Complexity: , where 
 is the length of the answer, which is less than 
.
Comments
What does the variable "N" refer to in the editorial?
N refers to the Nth Fool's number. (This is the input given to you)
A specific
 I think.
"If this occurs, we must now remove all P420s from N." How can N be a specific n if we are subtracting its fools number from it to find the correct fools number? Can someone please help me understand this?