Sunday, 10 May 2015

UVA 11089 Fi-binary Number

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int kk=1, tc, n, m, fibo[50];
    string s;
    cin>>tc;
    fibo[0]=fibo[1]=1;
    for(int i=2;i<=45;i++)
        fibo[i]=fibo[i-1]+fibo[i-2];
    //cout<<fibo[45]<<endl;

    while(tc--)
    {
        cin>>n;
        //cout<<"Case "<<kk++<<": ";
        int pos=44;
        while(fibo[pos]>n) pos--;
        while(pos)
        {
            if(n>=fibo[pos])
            {
                cout<<"1";
                n-=fibo[pos];
            }
            else cout<<"0";
            pos--;
        }
        cout<<"\n";
    }

    return 0;
}

Tuesday, 5 May 2015

UVA 441 Lotto (POJ 2245, ZOJ 1089, FZU 1047, HDU 1342, SCU 1027)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int arr[15], now[7], n;
bool vis[15];

void backTrack(int sz, int pos)
{
    if(sz==6)
    {
        for(int i=0;i<5;i++)
            cout<<now[i]<<" ";
        cout<<now[5]<<"\n";
        return;
    }
    for(int i=pos;i<n;i++)
        if(!vis[i])
        {
            vis[i]=true;
            now[sz]=arr[i];
            backTrack(sz+1, i+1);
            vis[i]=false;
        }
    return;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=0;
    string s;
    while(cin>>n && n)
    {
        for(int i=0;i<n;i++)
            cin>>arr[i];
        if(kk) cout<<"\n";
        backTrack(0, 0);

        kk++;
    }
return 0;
}

UVA 562 Dividing coins (UVALive 5583, Regionals 1996 >> Europe - Northwestern)

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int arr[105], dp[105][50005], n, tot;

int func(int pos, int taken)
{
    if(pos>=n) return abs(taken - (tot-taken));

    int &ret=dp[pos][taken];
    if(ret!=-1) return ret;

    ret=min(func(pos+1, taken), func(pos+1, taken+arr[pos]));
return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1;
    string s;
    cin>>tc;
    while(tc--)
    {
        cin>>n;
        tot=0;
        for(int i=0;i<n;i++)
        {
            cin>>arr[i];
            tot+=arr[i];
        }
        SET(dp);
        cout<<func(0, 0)<<"\n";
    }
return 0;
}

UVA 324 Factorial Frequencies (POJ 1454, UVALive 5432, Regionals 1993 >> North America - North Central NA)

import java.math.BigInteger;
import java.util.Scanner;


public class Main {

 public static void main(String[] args) {
  // TODO Auto-generated method stub
  Scanner sc =  new Scanner(System.in);
  int[] arr = new int[12];
  
  int n;
  
  while(sc.hasNext())
  {
   BigInteger fact = BigInteger.ONE;
   for(int i=0;i<10;i++)
    arr[i]=0;
   n=sc.nextInt();
   if(n==0) break;
   for(int i=2;i<=n;i++)
    fact = fact.multiply(BigInteger.valueOf(i));
   String s = fact.toString();
   int sl = s.length();
   for(int i=0;i<sl;i++)
    arr[ s.charAt(i)-48 ]++;
   System.out.println(n+"! --");
   System.out.printf("   (0)%5d   (1)%5d   (2)%5d   (3)%5d   (4)%5d\n", arr[0], arr[1], arr[2], arr[3], arr[4]);
   System.out.printf("   (5)%5d   (6)%5d   (7)%5d   (8)%5d   (9)%5d\n", arr[5], arr[6], arr[7], arr[8], arr[9]);
   
   
  }
  

 }

}

UVA 10131 Is Bigger Smarter?

///     Raihan Ruhin
///     CSE, Jahangirnagar University.
///     Dhaka-Bangladesh.
///     id: raihanruhin (topcoder / codeforces / codechef / hackerrank / uva / uvalive / spoj), 3235 (lightoj)
///     mail: raihanruhin@ (yahoo / gmail / facebook)
///     blog: ruhinraihan.blogspot.com

#include<bits/stdc++.h>
using namespace std;

#define SET(a) memset(a,-1,sizeof(a))
#define CLR(a) memset(a,0,sizeof(a))
#define PI acos(-1.0)

#define MOD 1000000007
#define MX 100010

int path[1005], w[1005], s[1005], dp[1005], n;

int func(int pos)
{
    if(pos>=n) return 0;

    int &ret=dp[pos];
    if(ret!=-1) return ret;
    int mx=0;
    for(int i=0;i<n;i++)
        if(w[i]>w[pos] && s[i]<s[pos])
        {
            int val = func(i);
            if(val>mx)
            {
                mx=val;
                path[pos]=i;
            }
        }
    ret=mx+1;
return ret;
}

int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);
    int tc, kk=1;
    //string s;
    n=0;
    while(cin>>w[n]>>s[n])
    {
        n++;
    }
    SET(dp);
    SET(path);

    int mx=0, st;
    for(int i=0;i<n;i++)
    {
        int val=func(i);
        //cout<<val<<endl;
        if(val>mx)
        {
            st=i;
            mx=val;
        }
    }
    cout<<mx<<"\n";
    while(path[st]!=-1)
    {
        cout<<st+1<<"\n";
        st=path[st];
    }
    cout<<st+1<<"\n";
return 0;
}