UVa 136 – Ugly Numbers


/*
User ID: turing_13
Link : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=72
*/

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;

int main()
{
    double a,b,c,k,ara[2000];
    int count=0;
    for(a=0; a<31; a++)
        for(b=0; b<31; b++)
            for(c=0; c<31; c++)
            {
                k=pow(2,c)*pow(3,b)*pow(5,a);
                if(k>1000000000)
                {
                    k=0;
                      break;
                }
                else
                {
                    ara[count]=k;
                    count++;
                }
            }
    sort(ara,ara+count);
    printf("The 1500'th ugly number is %.lf.\n",ara[1499]);
    return 0;
}

Advertisements

UVa 113 – Power of Cryptography


/*
User ID: turing_13
Link : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=49
*/

#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int main()
{
    double n,p;
    while(scanf("%lf %lf", &n,&p)==2)
    {
        printf("%.lf\n", pow(p,1/n));
    }
    return 0;
}

UVa 111 – History Grading


/*
User ID: turing_13
Link : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=47
*/

#include<bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long

using namespace std;

int data[30];
int input[30];
int n;
int dp[30];

int func(int u)
{
    int &rfr=dp[u];
    if(rfr!=-1) return rfr;
    rfr=1;
    for(int i=u+1;i<=n;i++)
    {
        if(data[input[u]]<data[input[i]])
            rfr=max(rfr,func(i)+1);
    }
    return rfr;
}

int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);

    cin>>n;
    for(int i=1;i<=n;i++)
        {
            int inp;
            cin>>inp;
            data[i]=inp;
        }
    int tmp;
    while(cin>>tmp)
    {
        input[tmp]=1;
        for(int i=2;i<=n;i++)
        {
            cin>>tmp;
            input[tmp]=i;
        }
        ms(dp,-1);
        int ans=0;

    for(int i=1;i<=n;i++)
        ans=max(ans,func(i));
    cout<<ans<<endl;
    }
    return 0;
}



UVa 102 – Ecological Bin Packing


/*
User ID: turing_13
Link : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=38
*/

#include <bits/stdc++.h>

#define ll long long
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)

using namespace std;

int main()
{
    ///freopen("input.txt","r",stdin);
    ///freopen("output.txt","w",stdout);
    long long a1,a2,a3,b1,b2,b3,c1,c2,c3,ans,i,j,k,l;
    while(cin>>a1>>b1>>c1>>a2>>b2>>c2>>a3>>b3>>c3)
    {
        j=(a2+a3+c1+c3+b1+b2);
        k=j;
        l=1;
        j=(a2+a3+b1+b3+c1+c2);
        if(j<k)
        {
            k=j;
            l=2;
        }
        j=(c2+c3+a1+a3+b1+b2);
        if(j<k)
        {
            k=j;
            l=3;
        }
        j=(c2+c3+b1+b3+a1+a2);
        if(j<k)
        {
            k=j;
            l=4;
        }
        j=(b2+b3+a1+a3+c1+c2);
        if(j<k)
        {
            k=j;
            l=5;
        }
        j=(b2+b3+c1+c3+a1+a2);
        if(j<k)
        {
            k=j;
            l=6;
        }
        if(l==1)
            cout<<"BCG "<<k<<endl;
        else if(l==2)
            cout<<"BGC "<<k<<endl;
        else if(l==3)
            cout<<"CBG "<<k<<endl;
        else if(l==4)
            cout<<"CGB "<<k<<endl;
        else if(l==5)
            cout<<"GBC "<<k<<endl;
        else if(l==6)
            cout<<"GCB "<<k<<endl;
    }
    return 0;
}

UVA 100 – The 3n + 1 problem


/*
User ID: turing_13
Link : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=36
*/

#include<stdio.h>

main()
{
    unsigned long int a,b,c,d,e,f,n,max;
    while(scanf(" %lu %lu", &a, &b) !=EOF)
    {
        if(a<b)
        {
            c=a;
            d=b;
        }
        else
        {
            c=b;
            d=a;
        }
        for(max=0; c<=d; c++)
        {
            n=c;
            e=1;
            while(n!=1)
            {
                if(n%2 !=0)
                    n=3*n+1;
                else
                    n/=2;
                e++;
            }
            if(e>max)
                max=e;
        }
        printf("%lu %lu %lu\n", a,b,max);
    }
    return 0;
}

UVa 591 – Box of Bricks


/*
User ID: turing_13
Link : http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=532
*/

#include <iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>

using namespace std;

int main()
{
    int n,ara[55]= {0},i,avg,ans,b=0;
    while(scanf("%d",&n)==1 & n!=0)
    {
        b++;
        avg=ans=0;
        for(i=0; i<n; i++)
        {
            scanf("%d",&ara[i]);
            avg+=ara[i];
        }
        avg/=n;
        for(i=0; i<n; i++)
        {
            if(ara[i]>avg)
                ans+=(ara[i]-avg);
        }
        printf("Set #%d\n",b);
        printf("The minimum number of moves is %d.\n\n",ans);
    }
    return 0;
}

Light OJ 1249 – Chocolate Thief


/*
Link: http://www.lightoj.com/volume_showproblem.php?problem=1249
*/

#include <bits/stdc++.h>

#define pii pair <int,int>
#define sc scanf
#define pf printf
#define Pi 2*acos(0.0)
#define ms(a,b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define MP make_pair
#define oo 1<<29
#define dd double
#define ll long long
#define EPS 10E-10
#define ff first
#define ss second
#define MAX 10000
#define SZ(a) (int)a.size()
#define getint(a) scanf("%d",&a)
#define loop(i,n) for(int i=0;i<n;i++)
#define TEST_CASE(t) for(int z=1;z<=t;z++)
#define PRINT_CASE printf("Case %d: ",z)
#define all(a) a.begin(),a.end()
#define intlim 2147483648
#define inf 1000000
#define rtintlim 46340
#define llim 9223372036854775808
#define rtllim 3037000499
#define ull unsigned long long
#define I int

using namespace std;

/* Bits operation */
int Set(int n,int pos)  { return n = n | 1<<pos;}
bool check(int n,int pos) { return n & 1<<pos;}
int Reset(int n, int pos) { return n=n & ~(1<<pos);}

struct data
{
    string name;
    int cube=0;
};

bool test(data a, data b)
{
    return a.cube < b.cube;
}
int main()
{
    ///freopen("in.txt","r",stdin);
    ///freopen("out.txt","w",stdout);
    vector<data>v;
    int t,n;
    getint(t);
    TEST_CASE(t)
    {
        data temp;
        int a,b,c;
        getint(n);
        loop(i,n)
        {
            cin>>temp.name;
            getint(a);
            getint(b);
            getint(c);
            temp.cube=a*b*c;
            v.pb(temp);
        }
        sort(all(v),test);
        PRINT_CASE;
        if(v[0].cube==v[n-1].cube)
            pf("no thief\n");
        else
        {
            cout<<v[n-1].name<<" ";
            pf("took chocolate from ");
            cout<<v[0].name<<endl;
        }
        v.clear();
    }
    return 0;
}