Uva 547 - DDF

/*

Solution Method: DP

Verdict : Accepted
Time: 0.040

*/


// Header file begin
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<cmath>
#include<cctype>
#include<sstream>
#include<set>
#include<list>
#include<stack>
#include<utility>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<bitset>
// End
//..........
// Macro
#define sf scanf
#define pf printf
#define lp1(i,n) for(i=0;i<n;i++)
#define lp2(i,n) for(i=1;i<=n;i++)
#define mset(c,v) memset(c,v,sizeof(c))
#define outs(a) cout<<" "<<a<<" "
#define nl puts("")
#define sq(x) ((x)*(x))
#define all(x) x.begin(),x.end()
#define reall(x) x.rbegin(),x.rend()
#define s_wap(x,y) x^=y;y^=x;x^=y;
#define sz size()
#define gc getchar()
#define psb push_back
#define freader freopen("input.txt","r",stdin)
// End.........

// Size
#define mx7 20000100
#define mx6 1500000
#define mx5 100005
#define mx4 1000100
#define inf 1<<30                                           //infinity value
#define eps 1e-9
#define mx (65540)
#define mod 1000000007
#define pi acos(-1.0)

// Macros for Graph

#define white 1
#define gray 2
#define black 3
#define nil 0

using namespace std;
/***************/

// typedef

typedef long long LL;
typedef long L;
typedef unsigned long long ull;
typedef unsigned long ul;
typedef unsigned int ui;
typedef pair<int, int> pii;
typedef vector<int>vi;
typedef vector<long long> vll;
typedef vector<long>vl;
typedef vector<char>vch;
typedef vector<string>vs;
typedef map<int,int>mpii;
typedef map<int,bool>mpbi;
typedef map<long,bool>mpbl;
typedef map<long long,bool>mpbll;
typedef map<char,int>mpci;
typedef map<char,bool>mpbc;
typedef map<string,int>mpsi;
typedef map<long long,long long>mpll;

// template

template<class T> T gcd(T a, T b ) {return b!=0?gcd<T>(b,a%b):a;}
template<class T> T large(T a, T b ) {return a>b?a:b;}
template<class T> T small(T a, T b ) {return a<b?a:b;}
template<class T> T diffrnce(T a, T b) {return a-b<0?b-a:a-b;}

struct my
{
    int val, tcnt;
};

my myarr[3000+5];

int digit_sum(int nm)
{
    int sm=0;

    while(nm)
    {
        sm+=(nm % 10);
        nm /= 10;
    }

    return sm;
}

void precalculate()
{
    int i, n , cnt=0, sum=0 , j;

    for(i=1; i<=3000; i++)
    {
        n = i;
        cnt=0;
        sum=0;

        while(n!=15 and n!=1)
        {
            for(j=1; j*j<=n; j++)
            {
                if(!(n % j))
                {
                    if(j != (n/j))
                    {
                        sum+=(digit_sum(j) + digit_sum(n/j));
                    }

                    else
                    {
                        sum+=digit_sum(j);
                    }
                }
            } //outs(sum);

            cnt++;

            n = sum;
            sum = 0;
        }

        myarr[i].val=i;
        myarr[i].tcnt = cnt;
    }

    //for(i=200; i<=300; i++)cout << myarr[i].val << " " << myarr[i].tcnt << "\n";
}

void get_solution(int n)
{
    int s=0;

    //cout << n;
    pf("%d",n);

    while(n!=15 and n!=1)
    {
        for(int i=1; i*i<=n; i++)
        {
            if(!(n%i))
            {
                if(i != (n/i))
                {
                    s+=(digit_sum(i) + digit_sum(n/i));
                }

                else
                {
                    s+=digit_sum(i);
                }
            }
        }

        //cout << " " << s;
        pf(" %d",s);

        n = s;
        s = 0;
    }
}

int main()
{
    precalculate();

    int from, to , tc=0;

    while(~sf("%d %d",&from,&to))
    {
        int i, maxi = -1;

        pf("Input%d: %d %d\n",++tc, from, to);

        if(from > to)
        {
            from ^= to;
            to ^= from;
            from ^= to;
        }

        for(i=from; i<=to; i++)
        {
            if(myarr[i].tcnt > maxi)
            {
                maxi = myarr[i].tcnt;
            }
        }

        pf("Output%d: ",tc);

        for(i=from; i<=to; i++)
        {
            if(maxi == myarr[i].tcnt)
            {
                get_solution(myarr[i].val);
                break;
            }
        }

//        cout << "maxi = ";
//        outs(maxi);
//
//        int x = b_search(from, to-1, maxi); //cout << "x = " << x;
//        cout << "index = " << myarr[x].val << "\n";
//
//        //get_solution(x);

        nl;
    }

    return 0;
}

======================= Another Solution ===================================

#include<bits/stdc++.h>

#define mx 3100

using namespace std;

int ar[mx];

int digit_sum(int n)
{
    int s=0;

    while(n)
    {
        s+=(n%10);
        n/=10;
    }

    return s;
}

int it(int n)
{
    int sum=0;

    for(int i=1; i*i<=n; i++)
    {
        if(!(n % i))
        {
            if(i == (n / i))
            {
                sum+=(digit_sum(i));
            }

            else
            {
                sum+=(digit_sum(i) + digit_sum(n/i));
            }
        }
    }

    return sum;
}

void DDF()
{
    int i , num=0 , cnt=0;

    for(i=1; i<mx; i++)
    {
        num=i;
        cnt=0;

        while(num > 1)
        {
            num = it(num);
            cnt++;
            if(num == 15)
            {
                break;
            }
        }

        ar[i] = cnt;
    }

    //for(i=1; i<=10; i++) cout << ar[i] << " ";
}

int main()
{
    ios_base::sync_with_stdio(false);
    DDF();
    int x,y,tc=0;
    while(cin >> x >> y)
    {
        cout << "Input" << ++tc << ": " << x << " " << y << "\n";

        if(x > y)
        {
            swap(x,y);
        }

        int maxi = -1 , in=0;

        for(int i=x; i<=y; i++)
        {
            if(ar[i] > maxi)
            {
                maxi = ar[i];
                in = i;
            }
        }

        //cout << in << "\n";

        cout << "Output" << tc << ": ";

        cout << in;

        while(in > 1)
        {
            if(in == 15)break;

            in = it(in);

            cout << " " << in;
        }

        cout << "\n";
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number