Monday, March 14, 2016

UVa 371 - Ackermann Function

#include <bits/stdc++.h>
#define LL  long long
#define SZ(x) ((int)(x).size())
#define ALL(x) (x).begin(),(x).end()
#define REP(i,n) for(int i=0;i<n;i++)
#define REV(i,n) for(int i=n-1;i>=0;i--)
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define pri(a) cout<<a<<endl
#define prii(a,b) cout<<a<<" "<<b<<endl
#define priii(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
#define hii printf("Hello World\n");
#define pcs printf("CASE# %d:\n", ++cs);
#define WRITE(fn) freopen(fn, "w", stdout);
using namespace std;

/****/ int cs = 0;
const int INF = 1<<29;
const int MX  = 1e5+10;

int main()
{
    LL lo,hi;
    LL val,tot,mx,ind;

    while(cin>>lo>>hi and lo+hi)
    {
        if(hi < lo) swap(hi,lo);

        mx  = -1;
        LL vv, cnt = 0;

        FOR(i,lo,hi)
        {
            vv = i;
            cnt = 0;

            if(vv == 1)
            {
                cnt = 3;
                if(cnt>mx) mx = cnt, ind = i;
                continue;
            }

            while(vv!=1)
            {
                if(vv%2)
                {
                    vv = 3*vv + 1;
                }
                else
                {
                    vv /= 2;
                }
                cnt++;
            }
            if(cnt>mx) mx = cnt, ind = i;
        }
        printf("Between %lld and %lld, %lld generates the longest sequence of %lld values.\n", lo, hi, ind, mx);
    }
    return 0;
}

No comments:

Post a Comment