UVa 11567 – Moliu Number Generator

#include <cstdio>
#include <algorithm>

using namespace std;

unsigned int solve (unsigned int i){

    unsigned int count = 1;
    while (i>1){
        if ((i&1)>0)
            return (count  + min(solve(i+1),solve(i-1)));
        count+=1+(1&i);
        i>>=1;
    }
    return count;
}

int main()
{
    unsigned int n;
    while (scanf("%d",&n)==1){
        if (n)
            printf("%d\n",solve(n));
        else
            printf("0\n");

    }

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s