UVALive - 3621 Power Calculus
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <queue>
#define N 300
#define M 20000
#define INF 99999999
using namespace std;
int n;
int num[1010];
int step;
int Find(int ans)
{
if(step>ans) return 0;
if(num[step] == n) return 1;
if(num[step]<<(ans - step) < n)
return 0;
for(int i=0;i<=step;i++)
{
step ++;
num[step] = num[i] + num[step -1];
if(num[step] <=1000&&Find(ans))
return 1;
num[step] = num[step-1] - num[i];
if(num[step] > 0&&Find(ans))
return 1;
step --;
}
return 0;
}
void Solve()
{
memset(num,0,sizeof(num));
int ans = 0;
step = 0;
while(1)
{ memset(num,0,sizeof(num));
num[0] = 1;
if(Find(ans))
{
break;
}
ans ++;
}printf("%d\n",ans);
}
int main()
{
while(scanf("%d",&n)==1&&n)
{
Solve();
}
return 0;
}