Saturday, 30 June 2012

UVa 200 - Rare Order Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007




int main()
{
    string s[MX];
    map<char,int>mp;
    char c;
    int cnt=0;
    while(cin>>s[cnt++])
        if(s[cnt-1]=="#")
            break;

    for(int i=0;i<20;i++)
        for(int j=0;j<cnt-1;j++)
            if(i<s[j].length())
                {
                    c=s[j][i];
                    if(mp[c]!=1)
                    {
                        cout<<c;
                        mp[c]=1;
                    }
                }
    cout<<endl;

return 0;
}

UVa 386 - Perfect Cubes Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007




int main()
{
    int qa,qb,qc,qd,qsum;

    for(int i=6;i<=200;i++)
    {
        qa= (int) pow((double) i, 3);
        for(int j=2;j<=200;j++)
        {
            qb= (int) pow((double) j, 3);
            for(int k=j+1;k<=200;k++)
            {
                qc= (int) pow((double) k, 3);
                for(int l=k+1;l<=200;l++)
                {
                    qd= (int) pow((double) l, 3);
                    qsum=qb+qc+qd;
                    if(qa == qsum)
                        cout<<"Cube = "<<i<<", Triple = ("<<j<<","<<k<<","<<l<<")"<<endl;
                }
            }
        }
    }

return 0;
}

UVa 10220 - I Love Big Numbers ! Solution

import java.util.*;
import java.math.*;
class Main
{
    public static void main(String[] args)
    {
        int n,i;
        long ans;
        BigInteger sum,tmp;
        Scanner scan=new Scanner(System.in);
        while(scan.hasNextInt())
        {
            n=scan.nextInt();
            ans=0;
            sum= BigInteger.valueOf(1);
            for(i=2;i<=n;i++)
                {
                    tmp = BigInteger.valueOf(i);
                    sum=sum.multiply(tmp);
                }

            String result = sum.toString();

            char[] characters=result.toCharArray();

            for(i=0; i<result.length(); i++)
                ans +=characters[i]-'0';

            System.out.println(ans);
        }
    }
}

UVa 10784 - Diagonal Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007




int main()
{
    LL ans, n,kk=1;
    double d;
    while(cin>>n && n)
    {
        d = (3.0 + sqrt( 9.0 + (n*8.0)))/2.0;
        ans=d;
        if(ans!=d)
            ans++;
        cout<<"Case "<<kk++<<": "<<ans<<endl;
    }

return 0;
}

UVa 10700 - Camel trading Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(LL i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007

LL multi_first(string line)
{
    stack<LL>stk;
    istringstream ss(line);
    LL num;
    LL mn=0;
    char c;
    ss>>num;
    stk.push(num);
    while(ss>>c)
    {
        if(c=='*')
        {
            LL x=stk.top();
            stk.pop();
            ss>>num;
            stk.push(num*x);
        }
        else
        {
            ss>>num;
            stk.push(num);
        }
    }

    while(!stk.empty())
        {
            mn+=stk.top();
            stk.pop();
        }
    return mn;

}

LL add_first(string line)
{
    stack<LL>stk;
    istringstream ss(line);
    LL num;
    LL mx=1;
    char c;
    ss>>num;
    stk.push(num);
    while(ss>>c)
    {
        if(c=='+')
        {
            LL x=stk.top();
            stk.pop();
            ss>>num;
            stk.push(num+x);
        }
        else
        {
            ss>>num;
            stk.push(num);
        }
    }

    while(!stk.empty())
        {
            mx*=stk.top();
            stk.pop();
        }
    return mx;

}


int main()
{
    LL mn,mx,tmp;
    LL t,num;
    char c;
    string line;
    cin>>t;
    getchar();
    while(t--)
    {
        getline(cin,line);

        //istringstream ss(line);
        //cout<<multi_first(line)<<endl;
        //cout<<add_first(line)<<endl;

        cout<<"The maximum and minimum are "<<add_first(line)<<" and "<<multi_first(line)<<"."<<endl;
    }
return 0;
}

Wednesday, 27 June 2012

UVa 10790 - How Many Points of Intersection? Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007




int main()
{
    LL a,b,suma,sumb,t,kk=1;
    while(cin>>a>>b)
    {
        if(a==0 && b==0)
            break;

        suma=(a*(a-1))/2;
        sumb=(b*(b-1))/2;
        cout<<"Case "<<kk++<<": "<<suma*sumb<<endl;
    }
return 0;
}

Monday, 18 June 2012

UVa 12470 - Tribonacci Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>


using namespace std;
#define MOD 1000000009

struct A{
    long long arr[10][10];
};

A MatrixMulti(A a, A b)
{
    A result;
    int i,j,k;
    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
        {
            result.arr[i][j]=0;
            for(k=0;k<3;k++)
                result.arr[i][j]+=(a.arr[i][k]*b.arr[k][j]);
            result.arr[i][j]=result.arr[i][j]%MOD;
        }
    return result;
}

A BigMod(A a, long long n)
{
    A ret;
    int i,j;
    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            {
                if(i==j)
                ret.arr[i][j]=1;
                else
                ret.arr[i][j]=0;
            }

    while(n)
    {
        if(n & 1)
    ret=MatrixMulti(ret,a);
        a=MatrixMulti(a,a);
        n>>=1;
    }
    return ret;
}

int main()
{
    A initialMatrix,ans;
    long long n,m,i,j;

    for(i=0;i<3;i++)
        for(j=0;j<3;j++)
            initialMatrix.arr[i][j]=1;

    initialMatrix.arr[1][1]=initialMatrix.arr[1][2]=initialMatrix.arr[2][0]=initialMatrix.arr[2][2]=0;
    while(cin>>n && n)
    {
       
            if(n==1)
            {
                cout<<"0"<<endl;
                continue;
            }
            if(n==2)
            {
                cout<<"1"<<endl;
                continue;
            }


            ans=BigMod(initialMatrix,n-3);
            cout<<(ans.arr[0][0]*2+ans.arr[0][1])%MOD<<endl;
    }
return 0;
}

UVa 12468 - Zapping Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;
int main()
{
    long long a,b,c,d,ans;
    while(cin>>a>>b)
    {
        if(a==-1 && b==-1)  break;

        c=abs(a-b);
        if(c>=50)
        c=100-c;
        cout<<c<<endl;

    }
return 0;
}

UVa 12465 - The Turanga Leela Problem Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;
int main()
{
    long long a,b,c,d,ans;
    while(cin>>a>>b)
    {
        if(!a && !b)  break;
        c=abs(a-b);
        d=sqrt(c);

        ans=0;
        for(int i=1;i<=d;i++)
            if(c%i==0)
                ans+=2;

        if(d*d==c)
            ans--;

        cout<<ans<<endl;

    }
return 0;
}

UVa 12464 - Professor Lazy, Ph.D. Solution

#include<iostream>
using namespace std;
int main()
{
    long long a,b,c;
    while(cin>>a>>b>>c)
    {   if(!a && !b && !c)  break;
        c=c%5;
        if(c==0)
            cout<<a<<endl;
        else if(c==1)
            cout<<b<<endl;
        else if(c==2)
            cout<<(b+1)/a<<endl;
        else if(c==3)
            cout<<(a+b+1)/(a*b)<<endl;
        else if(c==4)
            cout<<(a+1)/b<<endl;
    }
return 0;
}

UVa 12463 - Little Nephew Solution

#include<iostream>
using namespace std;
int main()
{
    long long a,b,c,d,e;
    while(cin>>a>>b>>c>>d>>e && a && b && c && d &&e)
    {
        cout<<a*b*c*d*d*e*e<<endl;
    }
return 0;
}

Friday, 15 June 2012

UVa 12469 - Stones Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007


bool isfib[1000+10];
int fib[20],n;

int main()
{
    fib[0]=0;
    fib[1]=1;
    for(int i=2;i<18;i++)
    {
        int x=fib[i]=fib[i-1]+fib[i-2];
        isfib[x]=true;
    }

    while(cin>>n && n)
    {
        if(n==1 || !isfib[n])
            cout<<"Alicia"<<endl;
        else
            cout<<"Roberto"<<endl;
    }

return 0;
}

UVa 12461 - Airplane Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<set>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define MOD 1000000007


int main()
{
    int n;
    while(cin>>n && n)
        cout<<"1/2"<<endl;

return 0;
}

UVa 12462 - Rectangle Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100100


LL colorNum[MX][40];
LL n,c,t,kk=1,histogram[MX],i,lft[MX],rgt[MX],mx,color[MX];

int main()
{
    stack<LL>stk;
    while(cin>>n>>c &&n &&c)
    {
        for(i=1;i<=n;i++)
        scanf("%lld",&histogram[i]);

        CLR(colorNum);

        for(i=1;i<=n;i++)
        {
            scanf("%lld",&color[i]);
            long long cl=color[i];
            colorNum[i][cl]=(colorNum[i-1][cl])+1;

            for(int j=0;j<cl;j++)
            colorNum[i][j]=colorNum[i-1][j];
            for(int j=cl+1;j<c;j++)
            colorNum[i][j]=colorNum[i-1][j];

         }


        while(!stk.empty())
            stk.pop();

        stk.push(0);
        histogram[0]=0;

        for(i=1;i<=n;i++)
        {
            while(histogram[stk.top()]>=histogram[i])
                stk.pop();

             lft[i]=  stk.top()+1;
             stk.push(i);
        }

        while(!stk.empty())
            stk.pop();

        stk.push(n+1);
        histogram[n+1]=0;

        for(i=n;i>0;i--)
        {
            while(histogram[stk.top()]>=histogram[i])
                stk.pop();

             rgt[i]=  stk.top()-1;
             stk.push(i);
        }
        mx=0;
        for(i=1;i<=n;i++)
            {
                bool chk=true;;
                for(int j=0;j<c;j++)
                    if(colorNum[rgt[i]][j] - colorNum[lft[i]-1][j]==0)
                        chk=false;
                if(chk)
                mx=max(mx,(rgt[i]-lft[i]+1)*histogram[i]);
            }
          cout<<mx<<endl;
        }
return 0;
}

Wednesday, 13 June 2012

Websites to find Crack, Keygen or Serial Key

*** http://www.serialkey.net/

*** http://www.keygenguru.com/

*** http://www.zcrack.com/

*** http://www.supercracks.net/

  http://www.serialportal.com/

  http://www.serialcrackz.com/

  http://www.serials.be/

  http://www.smartserials.com/

  http://www.cracksfm.com/

  http://www.cracklib.net/
  http://www.crackdb.org/

  http://www.keygens.nl/

  http://www.crackfind.com/

  http://www.crackserialkeygen.com/

  http://www.keygenmusic.net/

  http://www.cracktop.com/

  http://www.crack-keygen-site.com/

UVa 315 Network Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long
#define MX 100000
#define INF 2000000000
#define MOD 1000000007

bool visit[110];
vector<long>edge[110];
int n;

void dfs(int node)
{
    visit[node]=true;
    for(int j=0;j<edge[node].size();j++)
        if(!visit[edge[node][j]])
            dfs(edge[node][j]);
}

int Articulation_point()
{
    for(int i=1;i<=n;i++)
        if(!visit[i])
            return 1;
    return 0;
}


int main()
{
    int i, f, t,ans;
    string line;
    //READ("input.txt");
    //WRITE("a.txt");
    while(cin>>n)
    {
        if(n==0)
            break;


        if(n==1)
            {
                cin>>f;
                cout<<"0"<<endl;
                continue;
            }
        getchar();


        while(getline(cin,line))
        {
            istringstream ss(line);
            ss>>f;
            if(f==0)
                break;
            while(ss>>t)
            {
                edge[f].PB(t);
                edge[t].PB(f);
            }
        }

        ans=0;

        for(i=2;i<=n;i++)
        {
            CLR(visit);
            visit[i]=true;
            dfs(1);
            ans+=Articulation_point();
        }

        CLR(visit);
        visit[1]=true;
        dfs(2);
        ans+=Articulation_point();

        cout<<ans<<endl;

        for(i=1;i<=n;i++)
            edge[i].clear();
    }
return 0;
}

Sunday, 10 June 2012

UVa 10817 Headmaster's Headache Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long

int cost[110],n,s,m;
vector<int>sub[110];

int memo[110][1<<9][1<<9];

int func(int pos, int bitmask1, int bitmask2)
{
    if(  ( bitmask1==((1<<s)-1)) && (bitmask2==((1<<s)-1)))
        return 0;
    if(pos>n)   return 10000000;

    if(memo[pos][bitmask1][bitmask2]!=-1)  return memo[pos][bitmask1][bitmask2];

    int tmp1, tmp2;
    tmp1=bitmask1;
    tmp2=bitmask2;

    for(int i=0;i<sub[pos].size();i++)
    {
        int x=sub[pos][i];

        if((tmp1 & (1<<x))==0)
               tmp1 = tmp1 | (1<<x);
        else
            tmp2 = tmp2 | (1<<x);
    }

    return memo[pos][bitmask1][bitmask2]=min(func(pos+1, bitmask1, bitmask2), cost[pos]+func(pos+1, tmp1, tmp2));
}

int main()
{
    string s_line;
    int x,i,mask1,mask2,ans,cst;
    while(cin>>s>>m>>n)
    {
        if(s==0) break;
        s++;
        ans=0;
        mask1=1;
        mask2=1;

        for(i=1;i<=m;i++)
        {
            cin>>cst;
            ans+=cst;
            getline(cin, s_line);
            istringstream ss1(s_line);

            while(ss1>>x)
                {
                   if((mask1 & (1<<x))==0)
                        mask1 = mask1 | (1<<x);
                    else
                        mask2 = mask2 | (1<<x);
                }
        }

        for(i=1;i<=n;i++)
        {
            cin>>cost[i];
            getline(cin, s_line);
            istringstream ss(s_line);

            while(ss>>x)
                sub[i].PB(x);
        }


        SET(memo);

        ans+=func(1, mask1, mask2);
        cout<<ans<<endl;
        for(i=1;i<=n;i++)
            sub[i].clear();

    }
}

Friday, 8 June 2012

UVa 446 Kibbles `n' Bits `n' Bits `n' Bits Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long

string DecToBin(int number)
{
    if ( number == 0 ) return "0";
    if ( number == 1 ) return "1";

    if ( number % 2 == 0 )
        return DecToBin(number / 2) + "0";
    else
        return DecToBin(number / 2) + "1";
}


int main()
{
    LL hex1,hex2,n,ans;
    char c;
    string s;
    cin>>n;
    while(n--)
    {
        cin>>hex>>hex1>>c>>hex>>hex2;
        s=DecToBin(hex1);
        stringstream ss;
        ss<<s;
        ss>>ans;
        printf("%013lld %c ",ans,c);

        s=DecToBin(hex2);
        stringstream ss2;
        ss2<<s;
        ss2>>ans;
        printf("%013lld = ",ans);

        if(c=='-')
        cout<<hex1-hex2<<endl;
        else
        cout<<hex1+hex2<<endl;

    }

return 0;
}

Sunday, 3 June 2012

UVa 10496 Collecting Beepers Solution

#include<iostream>
#include<list>
#include<string>
#include<cstring>
#include<sstream>
#include<cctype>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<stack>
#include<fstream>
#include<cstdlib>
#include<vector>
#include<map>
#include<utility>
#include<iomanip>
#include<queue>

using namespace std;

#define INF (1<<29)
#define SET(a) memset(a,-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define CLR(a) memset(a,0,sizeof(a))
#define FILL(a,v) memset(a,v,sizeof(a))
#define PB push_back
#define FOR(i,n) for(int i = 0;i<n;i++)
#define PI acos(-1.0)
#define EPS 1e-9
#define MP(a,b) make_pair(a,b)
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
#define LL long long

struct Z{
    int x,y;
}beeper[12];

int memo[12][1<<11],beepersNum;

int func(int prev, int bitmask)
{
    if(bitmask==(1<<beepersNum)-1)
        return abs(beeper[prev].x-beeper[0].x)+abs(beeper[prev].y-beeper[0].y);

    if(memo[prev][bitmask]!=-1)  return memo[prev][bitmask];

    int ret=1000000;

    for(int i=0;i<beepersNum;i++)
        if((bitmask & (1<<i))==0)
            ret=min(ret, func(i, bitmask | (1<<i))+abs(beeper[i].x-beeper[prev].x)+abs(beeper[i].y-beeper[prev].y));

    return memo[prev][bitmask]=ret;
}

int main()
{
    int tc,faltu;
    cin>>tc;
    while(tc--)
    {
        cin>>faltu>>faltu;
        cin>>beeper[0].x>>beeper[0].y;
        cin>>beepersNum;
        beepersNum++;

        for(int i=1;i<beepersNum;i++)
            cin>>beeper[i].x>>beeper[i].y;

        SET(memo);

        cout<<"The shortest path has length "<<func(0,1)<<endl;
    }
return 0;
}

UVa 11614 Etruscan Warriors Never Play Chess Solution

# include <stdio.h>
# include <math.h>
int main()
{
    long long i,n,t,sum;
   scanf("%lld",&t);
   for(i=1;i<=t;i++)
   {
       scanf("%lld",&n);
       sum = (sqrt(1+8*n)-1)/2;
       printf("%lld\n",sum);
   }
    return 0;
}

UVa 11483 Code Creator Solution

#include <cstdio>

char line[2048], ans[2048];
int main(void){
 int N, cnum = 0;
 while(scanf("%d",&N) && N){
  printf("Case %d:\n", ++cnum);
  puts("#include<string.h>\n#include<stdio.h>\nint main()\n{");
  while(N--){
   fgets(line, 2048, stdin);
   if(line[0] == 10){ N++; continue;}
   register char *p, *o = ans;
   fputs("printf(\"",stdout);
   for(p = line; *p != 10; p++){
    if(*p == '\\' || *p == '\"') *o++ = '\\';
    *o++ = *p;
   }
   *o++ = '\\'; *o++ = 'n'; *o++ = '\"'; *o++ = ')';
   *o++ = ';'; *o = 0;
   puts(ans);
  }
  puts("printf(\"\\n\");\nreturn 0;\n}");
 }
 return 0;
}

UVa 12157 Tariff Plan Solution

#include <cstdio>

int main(void){
 int T, N, mile, juice, x, cnum = 0;
 scanf("%d",&T);
 while(T--){
  scanf("%d",&N);
  mile = juice = 0;
  for(int i = 0; i < N; ++i){
   scanf("%d",&x);
   mile += (x + 30)/30;
   juice += (x + 60)/60;
  }
  mile *= 10;
  juice *= 15;
  printf("Case %d: ",++cnum);
  if(mile < juice) printf("Mile %d\n",mile);
  else if(juice < mile) printf("Juice %d\n",juice);
  else printf("Mile Juice %d\n",mile);
 }
 return 0;
}

UVa 12019 Doom's Day Algorithm Solution

#include <cstdio>

const int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const char *wd[7] = {"Friday", "Saturday", "Sunday", "Monday", "Tuesday",
 "Wednesday", "Thursday"};
int cd[12];
int main(void){
 int t; scanf("%d", &t);
 cd[0] = days[0];
 for(int i = 1; i < 12; ++i) cd[i] = days[i] + cd[i - 1];
 for(int d, m; t-- && scanf("%d %d", &m, &d); ){
  int nday = d;
  if(--m) nday += cd[m - 1];
  //printf("%d %d -> %d\n", m, d, nday);
  puts(wd[nday % 7]);
 }
 return 0;
}

UVa 11945 Financial Management Solution

#include <clocale>
#include <cstdio>
int main(void){
 int T;
 scanf("%d", &T);
 setlocale(LC_ALL, "en_US.UTF-8");
 for(int cnum = 0; cnum++ < T; ){
  double r = 0, z;
  for(int i = 0; i < 12; ++i, r += z) scanf("%lf", &z);
  printf("%d $%'.2lf\n", cnum, r / 12.0);
 }
 return 0;
}

UVa 11723 Numbering Roads Solution

#include <cstdio>

int main(void){
 int R, N;
 for(int cnum = 0; (scanf("%d %d", &R, &N) && R); ){
  printf("Case %d: ", ++cnum);
  int s = (R - 1)/N;
  if(s > 26) printf("impossible\n");
  else printf("%d\n", s);
 }
 return 0;
}

UVa 11687 Digits Solution

#include <cstdio>
#include <cstring>
char digits(unsigned int x){
 register char ret = 0;
 while(x) x/=10, ret++;
 return ret;
}
int main(void){
 register char c;
 unsigned int cnt;
 bool mark;
 while((c = getc( stdin )) != 'E'){
  cnt = mark = 1;
  if(c == '1') mark = 0;
  while((c = getc( stdin )) != '\n') cnt++;
  if(cnt == 1) printf("%d\n",cnt+mark);
  else if(cnt < 10) printf("%d\n",3);
  else printf("%d\n",4);
 }
 return 0;
}

UVa 11661 Burger Time? Solution

    #include <cstdio>
    int main(void){
        register char c;
        int d, i, ld, lr, temp, L;
        while(scanf("%d\n",&L) && L){
            d = 1<<25;
            lr = ld = -1;
            for(i = 0; i < L; i++){
                c = getc( stdin );
                if(c == '.') continue;
                if(c == 'R'){
                    if(ld != -1){
                        temp = i - ld;
                        if(temp < d) d = temp;
                    }
                    lr = i;
                } else if(c == 'D'){
                    if(lr != -1){
                        temp = i - lr;
                        if(temp < d) d = temp;
                    }
                    ld = i;
                } else if(c == 'Z'){
                    d = 0;
                    break;
                }
            }
            while(++i <= L) getc( stdin );
            printf("%d\n",d);
        }
        return 0;
    }

UVa 11455 Behold my quadrangle Solution

# include <stdio.h>
int main()
{
int n,i,ar[5],j,k,temp;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d %d %d %d",&ar[0],&ar[1],&ar[2],&ar[3]);
if(ar[0]==ar[1] && ar[1]==ar[2] && ar[2]==ar[3])
printf("square\n");
else
{

for(j=0;j<3;j++)
{
for(k=0;k<3-j;k++)
{
if(ar[k]>ar[k+1])
{
temp = ar[k];
ar[k] = ar[k+1];
ar[k+1] = temp;
}

}


}
if(ar[0]==ar[1] && ar[2]==ar[3])
printf("rectangle\n");
else if((ar[0]+ar[1]+ar[2])>ar[3])
printf("quadrangle\n");
else
printf("banana\n");


}

}
return 0;
}

UVa 11389 The Bus Driver Problem Solution

#include<stdio.h>
void selection(int x[],int n);
int main()
{ int n,d,r,i,morning[100],evening[100],dtime,totaltime;
  while(scanf("%d%d%d",&n,&d,&r)==3)
  {
      if(n==0 && d==0 && r==0)
      break;
      totaltime=0;
      for(i=0;i<n;i++)
      scanf("%d",&morning[i]);
      for(i=0;i<n;i++)
      scanf("%d",&evening[i]);
      selection(morning,n);
      selection(evening,n);
      /*for(i=0;i<n;i++)
      printf("%d ",morning[i]);
      for(i=0;i<n;i++)
        printf("%d ",evening[i]); */
        for(i=0;i<n;i++)
        {
          dtime=morning[i]+evening[n-1-i];
          if(dtime>d)
          totaltime=totaltime+(dtime-d);
          }
         printf("%d\n",totaltime*r);

    }
    return 0;
    }



  void selection(int x[],int n)
  {
      int i,j,minvalue,temp;
      for(i=0;i<n-1;i++)
      {
         minvalue=i;
         for(j=i+1;j<n;j++)
         {
            if(x[minvalue]>x[j])
            {
              minvalue=j;
              }
          }
          if(minvalue!=i)
          {
            temp=x[i];
            x[i]=x[minvalue];
            x[minvalue]=temp;
            }
         }

      }

UVa 11369 Shopaholic Solution

#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
  int n,t,price[20001],i,j;
  long int discount;

  scanf("%d",&t);

  for(i=0;i<t;i++)
  {  discount=0;
      scanf("%d",&n);
      for(j=0;j<n;j++)
      scanf("%d",&price[j]);

      sort(price,price+n);
      for(j=n-3;j>=0;j=j-3)
      discount=discount+price[j];

      printf("%ld\n",discount);
      }
      return 0;
      }

UVa 11292 Dragon of Loowater Solution

     #include <cstdio>
    #include <set>
    using namespace std;
    multiset<int> k;
    int h[32768], n;
    int main(){
        multiset < int >::iterator it;
        for (int c, fail, m, n, v; scanf("%d %d",&n,&m)!=EOF && (n || m); ){
            k.clear(); c = fail = 0;
            for(int i = 0; i < n; ++i) scanf("%d", &h[i]);
            for(int i = 0; i < m; ++i) scanf("%d", &v), k.insert(v);
            for(int i = 0; i < n; ++i){
                if((it = k.lower_bound(h[i])) == k.end()){ fail = 1; break;}
                c += *it;
                k.erase(it);
            }
            if(fail) puts("Loowater is doomed!");
            else printf("%d\n", c);
        }
    }

UVa 11233 Deli Deli Solution

#include <stdio.h>
#include <string.h>
struct x
{
char word[100];
char pword[100];
}a[100];
int main()
{
int L,N;
while(scanf("%d %d",&L,&N)==2)
{
int i,j;
for(i=1;i<=L;i++)
scanf("%s %s",&a[i].word,&a[i].pword);
for(j=1;j<=N;j++)
{
char name[100];
scanf("%s",name);
int k=0;
for(i=1;i<=L;i++)
if(strcmp(name,a[i].word)==0)
{
printf("%s\n",a[i].pword);
k=1;
break;
}
if(k==0)
{
int c=strlen(name);
int b=c-1;
if(name[b]=='y' && (name[b-1]!='a'&& name[b-1]!='e'&& name[b-1]!='i'&& name[b-1]!='o' && name[b-1]!='u'))
{ name[b++]='i';
name[b++]='e';
name[b++]='s';
name[b]='\0';
printf("%s\n",name);
}
else if(name[b]=='o'|| name[b]=='s' ||(name[b]=='h' && name[b-1]=='c')|| ( name[b]=='h' && name[b-1]=='s')|| name[b]=='x')
{
name[c++]='e';
name[c++]='s';
name[c]='\0';
printf("%s\n",name);
}
else
{
name[c++]='s';
name[c]='\0';
printf("%s\n",name);
}
}
}

}
return 0;
}

UVa 10530 Guessing Game Solution

#include <stdio.h>
#include <string.h>
int main()
{
int n,a[12]={0},i;
char str[20];
while(scanf("%d",&n) && n)
{ getchar();
gets(str);

if(strcmp(str,"too low")==0)
{
for(i=n;i>=1;i--)
a[i]=-1;
}
else if(strcmp(str,"too high")==0)
{
for(i=n;i<=10;i++)
a[i]=-1;
}
else
{
if(a[n]==0)
printf("Stan may be honest\n");
else
printf("Stan is dishonest\n");
for(i=0;i<=10;i++)
a[i]=0;
}

}
return 0;
}

UVa 10450 World Cup Noise Solution

#include <stdio.h>
unsigned  long long int dp[51][2];
unsigned long long int nSequence(int n,int last)
{

 if(dp[n][last]!=-1)
  return dp[n][last];
 else if(last)
  return dp[n][last] =nSequence(n-1,0);
 else
  return dp[n][last] = nSequence(n-1,1) + nSequence(n-1,0);
}
int main()
{
 int i,c,cc;
 for(i=0;i<51;i++)
  dp[i][0]=dp[i][1]=-1;
 dp[1][0]=2;
 dp[1][1]=1;
 cc = 1;
 scanf("%d",&c);
 while(c--)
 {
  scanf("%d",&i);
  printf("Scenario #%d:\n",cc++);
  printf("%llu\n\n",nSequence(i,0));
 }
 return 0;
}

UVa 10409 Die Game Solution

#include<cstdio>
#include<string.h>
int main()
{
    int N;
    char side[10];
    int north,south,east,west,top,bottom,tempBottom,tempTop;
    while ((scanf("%d",&N)==1)&&N>0)
    {
        top = 1;
        bottom = 6;
        east = 4;
        west = 3;
        north = 2;
        south = 5;
        while (N--)
        {
            scanf("%s",&side);
            if (strcmp(side, "north") == 0)
            {
                tempBottom = bottom;
                tempTop = top;
                top = south;
                bottom = north;
                south = tempBottom;
                north = tempTop;
            }
            else if (strcmp(side, "south") == 0)
            {
                tempBottom = bottom;
                tempTop = top;
                top = north;
                bottom = south;
                south = tempTop;
                north = tempBottom;
            }
            else if (strcmp(side, "east") == 0)
            {
                tempBottom = bottom;
                tempTop = top;
                top = west;
                bottom = east;
                west = tempBottom;
                east = tempTop;
            }
            else if (strcmp(side, "west") == 0)
            {
                tempBottom = bottom;
                tempTop = top;
                top = east;
                bottom = west;
                east = tempBottom;
                west = tempTop;
            }
        }
        printf("%d\n",top);
    }
}

UVa 10388 Mischievous children Solution

#include <stdio.h>
#include <string.h>
char ascii[26];
char factorials[21];
unsigned long long int results[21];
int main()
{
    unsigned long long int res;
    register int i,j,cc;
    cc = 1;
    int c;
    char s[21];
    results[2]=2;
    for(i=3;i<21;i++)results[i]=results[i-1]*i;
    scanf("%d",&c);
    getchar();
    while(c-->0)
    {
        res = 1;
        gets(s);
        for(j=0;j<=20;j++)factorials[j]=0;
        for(i=0;i<26;i++)ascii[i]=0;
        i = strlen(s);
        for(j=0;j<i;j++)
            ascii[s[j]-'A']++;
        for(j=0;j<26;j++)
            factorials[ascii[j]]++;
        res = results[i];
        for(j=2;j<=20;j++)
            for(i=1;i<=factorials[j];i++)
                res/=results[j];
        printf("Data set %d: %llu\n",cc++,res);
    }
    return 0;
}

UVa 10295 Hay Points Solution

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct word{
string str;
int dollar;
}dics[1200];
inline bool comp(word a, word b){
if (a.str > b.str) return false;
return true;
}
int binarySearch(string a, int h){
int low, mid, high = h;
low = 0;
while (low <= high){
mid = (high + low) / 2;
if (dics[mid].str == a)
return mid;
else if (dics[mid].str < a)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(){
int m, n, k, i, dollar;
string a;
while (cin >> m >> n){
for (i = 0; i < m; ++i)
cin >> dics[i].str >> dics[i].dollar;
sort(dics, dics+m, comp);
for (i = 0; i < n; ++i){
dollar = 0;
while (cin >> a){
if (a == ".") break;
k = binarySearch(a, m);
if (k != -1)
dollar += dics[k].dollar;
}
cout << dollar << endl;
}
}
return 0;
}

UVa 10282 Babelfish Solution

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct w {
    char en[25], xx[25];
} word;
 
word dict[1000002] = {0,0};
 
int cmp(const void *a, const void *b) {
    return strcmp(((word *)a)->xx,((word *)b)->xx);
}
int main() {
    int I=0, J;
    char INPUT[100], *p;
    word *q, QUERY;
 
    while(gets(INPUT)) {
        if (!strcmp(INPUT,""))
            break;
        p = strtok(INPUT," ");
        strcpy(dict[I].en,p);
        p = strtok(NULL," ");
        strcpy(dict[I].xx,p);
        I++;
    }
 
    qsort(dict,I,sizeof(word),cmp);
 
    while(scanf("%s",QUERY.xx)!=EOF) {
        q = bsearch(&QUERY,dict,I,sizeof(word),cmp);
        if (q!=NULL) {
            printf("%s\n",q->en);
        } else {
            printf("eh\n");
        }
    }
 
    return 0;
}

UVa 10279 Mine Sweeper Solution

#include <iostream>
#include <string>
using namespace std;
char data[20][20];
char str[20][20];
char output[20][20];
int dr[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dc[] = {-1, 0, 1, 1, 1, 0, -1, -1};
void init(int n){
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j)
output[i][j] = '.';
}
}
char check(int n, int R, int C){
int i, r, c, num = 0;
for (i = 0; i < 8; ++i){
r = R + dr[i];
c = C + dc[i];
if ( r >= 0 && r < n && c >= 0 && c < n){
if (data[r][c] == '*')
++num;
}
}
return num+48;
}
void checkAll(int n){
for (int i = 0; i < n; ++i){
for (int j = 0; j < n; ++j){
if (data[i][j] == '*')
output[i][j] = '*';
}
}
}
int main(){
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
bool flag = false;
int test, i, j, t, n;
cin >> test;
while (test--){
cin >> n;
if (flag) cout << endl; flag = true;
init(n);
for (t = 0; t < n; ++t)
cin >> data[t];
for (t = 0; t < n; ++t)
cin >> str[t];
for (i = 0; i < n; ++i){
for (j = 0; j < n; ++j){
if (data[i][j] == '.' && str[i][j] == 'x'){
output[i][j] = check(n, i, j);
}
if (data[i][j] == '*' && str[i][j] == 'x'){
checkAll(n);
}
}
}
for (i = 0; i < n; ++i){
for (j = 0; j < n; ++j)
cout << output[i][j];
cout << endl;
}
}
return 0;
}