Collatz polynomial
면접 대비시간 제한0.5초메모리 제한2048 MB
계수가 0 또는 1인 다항식에 대해 (x+1)을 곱하고 1을 더하는 연산과 x로 나누는 연산을 번갈아 적용하며 1이 될 때까지의 연산 횟수를 세는 문제이다.
문제
Everyone knows (or has heard of) the famous Collatz Conjecture: take a positive integer. If it is odd, multiply by and add . If it is even, divide by . Repeat the process until you reach . Despite its simplicity, no one knows how to prove whether the sequence really always reaches , regardless of the initial number.
Aline, a fan of this type of curiosity, decided to create a variation using polynomials instead of numbers. To keep things simple, she works only with polynomials whose coefficients are or , that is, each power of appears at most once.
The game works like this:
- If the polynomial has a constant term (a term that does not depend on ), Aline multiplies the polynomial by and then adds . If any resulting coefficient equals , the corresponding term is discarded (note that coefficients greater than cannot arise).
- If the polynomial has no constant term, Aline divides the polynomial by .
This process is repeated until the polynomial reduces to .
Consider . In the first step there is a constant term, so we calculate:
.
Since the coefficient of the constant term is , this term is discarded, leaving:
.
Next, since there is no constant term, we divide by :
.
Continuing:
- Step :
- Step :
- Step :
- Step :
- Step :
- Step :
- Step :
- Step :
- Step :
In total, it took operations to reach the polynomial .
Aline needs help to study this variation of the Collatz Conjecture. Since doing these calculations manually is prone to errors, write a program that determines the number of operations needed until the polynomial becomes .
입력
The first line contains an integer (), indicating the degree of the polynomial.
The second line contains integers (each equal to or ), where indicates that the term is present in the polynomial, and indicates that it is not. Note that , since the degree of the polynomial is .
출력
Your program must output a single line, containing an integer, representing the number of operations needed until the polynomial becomes .