YOKOF - Power Calculus

no tags 

Starting with x and repeatedly multiplying by x, we can compute x31 with thirty multiplications:

x2 = x * x,     x3 = x2 * x,     x4 = x3 * x,     ...       x31 = x30 * x.

The operation of squaring can appreciably shorten the sequence of multiplications. The following is a way to compute x31 with eight multiplications:

x2 = x * x,     x3 = x2 * x,     x6 = x3 * x3,     x7 = x6 * x,     x14 = x7 * x7,
x15 = x14 * x,     x30 = x15 * x15,     x31 = x30 * x.

This is not the shortest sequence of multiplications to compute x31. There are many ways with only seven multiplications. The following is one of them:

x2 = x * x,     x4 = x2 * x2,     x8 = x4 * x4,     x10 = x8 * x2,
x20 = x10 * x10,     x30 = x20 * x10,     x31 = x30 * x.

There however is no way to compute x31 with fewer multiplications. Thus this is one of the most efficient ways to compute x31 only by multiplications.

If division is also available, we can find a shorter sequence of operations. It is possible to compute x31 with six operations (five multiplications and one division):

x2 = x * x,     x4 = x2 * x2,     x8 = x4 * x4,     x16 = x8 * x8,     x32 = x16 * x16,     x31 = x32 ÷ x.

This is one of the most efficient ways to compute x31 if a division is as fast as a multiplication.

Your mission is to write a program to find the least number of operations to compute xn by multiplication and division starting with x for the given positive integer n. Products and quotients appearing in the sequence of operations should be x to a positive integer's power. In other words, x-3, for example, should never appear.

Input

The input is a sequence of one or more lines each containing a single integer n. n is positive and less than or equal to 1000. The end of the input is indicated by a zero.

Output

Your program should print the least total number of multiplications and divisions required to compute xn starting with x for the integer n. The numbers should be written each in a separate line without any superfluous characters such as leading or trailing spaces.

Example

Input:
1
31
70
91
473
512
811
953
0

Output: 
0
6
8
9
11
9
13
12

hide comments
:D: 2018-10-20 13:36:15

Also check similar but easier problem GSMATRIX

Gregorius Edward: 2010-11-22 12:04:43

Solution for 953:
x^2 = x^1 * x^1, x^4 = x^2 * x^2
x^8 = x^4 * x^4, x^16 = x^8 * x^8
x^32 = x^16 * x^16,
x^64 = x^32 * x^32,
x^63 = x^64 / x,
x^127 = x^64 * x^63,
x^254 = x^127*x^127,
x^508 = x^254 * x^254,
x^1016 = x^508 * x^508,
x^953 = x^1016 / x^63.
12 steps

i still get WA, can someone give me another test case?

Ehor Nechiporenko: 2010-10-23 10:33:04

Solution for 473:
x^2 = x^1 * x^1, x^4 = x^2 * x^2
x^8 = x^4 * x^4, x^16 = x^8 * x^8
x^32 = x^16 * x^16,
x^31 = x^32 / x,
x^63 = x^32 * x^31,
x^126 = x^63 * x^63,
x^252 = x^126*x^126,
x^504 = x^252 * x^252
x^473 = x^504 / x^31
11 steps

But can somebody help with 953?

Last edit: 2010-10-23 10:36:39
The Champ: 2010-10-21 17:19:20

how x^463 took 11 steps, i am getting 12 as my least value for the same

Nikhil Garg: 2010-10-20 08:08:33

Could X for multiplication be changed to * or something else. Its not easy to read now.


Added by:Race with time
Date:2010-10-17
Time limit:2.537s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64 NODEJS OBJC VB.NET
Resource:ACM Yokohama 2006