Олимпиадный тренинг

Задача 31843. Binary sequences


Задача

Темы: Рекурсия
Number N is entered. Generate in lexicographic order all sequences of length N, consisting of numbers 2, 4, 5, in which the number of twos does not exceed 2.
 
In "lexicographical order" means that if two sequences coincide at the first X places, but differ at the X+1 place, then the one in which the number at the X+1 place is less should go first.
 
1≤N≤9

Examples
# Input Output
1 3
2 2 4
2 2 5
2 4 2
2 4 4
2 4 5
2 5 2
2 5 4
2 5 5
4 2 2
4 2 4
4 2 5
4 4 2
4 4 4
4 4 5
4 5 2
4 5 4
4 5 5
5 2 2
5 2 4
5 2 5
5 4 2
5 4 4
5 4 5
5 5 2
5 5 4
5 5 5
 

Запрещенные операторы:for;while;until