Friday, 24 July 2015

FARIDA



Princess Farida

Link to the question : FARIDA 

HINT :

The question asks us to take the maximum number of gold coins from the monsters such that if m_1, m_2, m_3......m_n are the monsters then  you can take gold coin from monster m_i only if you had not taken from m_(i-1).
This can be solved very easily by dp. Let us take an array b[] such that b[i] stores the maximum number of gold coins you can get from m_1 to m_i monsters. So,
b[i] = max( gold coins taken from ith monster + b[j]) where 0<j<i-1 .

RECOMMENDED QUESTION :

After solving this dp question, you would love solving this question as well.

SOURCE CODE :


/* Princess Farida */

/* Sushant Gupta */



#include<iostream>

using namespace std;

int main()

{

    int t,k=1;

    cin>>t;

    while(t--)

    {

        int n,i,j;

        cin>>n;

        long long int a[n],b[n],max=0;



        for(i=0;i<n;i++)

        {

            cin>>a[i];

            b[i]= 0;

        }

        b[0]= a[0];

        b[1]= a[1];

        for(i=2;i<n;i++)

        {

            for(j=0;j<i-1;j++)

            {

                if(b[i]< b[j] + a[i])

                    b[i]= b[j] + a[i];

            }

        }

        for(i=0;i<n;i++)

        {

            if(max< b[i])

                max = b[i];

        }

        cout<<"Case "<<k++<<": "<<max<<endl;

    }



    return 0;

}



  

BANDW


Black and White

Link to the question : BANDW 

HINT :

My solution is very basic.Let the input string be s[] and the objestive string be t[].  Run a loop till the end. If s[i] = t[i] continue, else if they are not equal, keep a count again when they are equal.
I think checking the source code once will make it more clear.

RECOMMENDED QUESTION :

Try this adhoc question after this one.

SOURCE CODE :

/* Black And White */
/* Sushant Gupta */

#include<stdio.h>
#include<string.h>
int main()
{
    char s[600],t[600];
    int i;
    s[0]= '0';

    while(s[0]!= '*')
    {
        scanf("%s%s",s,t);
        if(s[0]!= '*')
        {
            int n,c=0;
            n = strlen(s);
            i=0;
            while(i<n)
            {
                if(s[i]!=t[i]) {
                while(s[i]!=t[i])
                    i++;
                c++;}
                while(s[i]==t[i])
                    i++;
            }
            printf("%d\n",c);
        }
        else
            return 0;
    }
}
 

Thursday, 23 July 2015

FANCY



FANCY NUMBERS

Link to the question : FANCY 

HINT :

A very good question on permutation and combination. 
If we take for example the test case given in the question : 11112
The answer depends on the number of possible combinations of 1111 which satisfies the rules of the question.
Hence the number of combinations of 1111 is 2^3. As our combinations will depend on whether we choose a particular 1.

RECOMMENDED QUESTION :

After solving this pnc question, you may like solving this adhoc question .

SOURCE CODE :

#include<stdio.h>
#include<math.h>
#include<string.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char a[50];
        int i,c,j;
        long long int ans=1;
        scanf("%s",a);
        i=0;
        j=strlen(a);

        while(i<j)
        {    c=0;

            while(a[i+1]==a[i])
            {
                c++;
                i++;
            }
            ans=ans * pow(2,c);
            i++;
        }
        printf("%lld\n",ans);
    }
    return 0;
}
 

ENIGMATH


PLAY WITH MATH

Link to the question : ENIGMATH 

HINT :

A question similar to CEQU. 
In order for x and y to satisfy the equation a*x = b*y , a*x should be equal to the lcm of (a,b). Similarly, b*y should also be equal to the lcm of the coefficients.

RECOMMENDED QUESTION :

A similar question to this : CEQU.
You will surely enjoy solving it.

SOURCE CODE :

 #include <stdio.h>
#include<math.h>

int main()
{
    int t;
    long long int a,s,b,i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&a,&b);
        while(a%2==0 && b%2==0)
        {
            a=a/2;
            b=b/2;
        }
        if(a>b)
            s=b;
        else
            s=a;
        for(i=3;i<=sqrt(s);i=i+2)
        {
            while(a%i==0 && b%i==0)
            {
                a=a/i;
                b=b/i;
            }
        }
        printf("%lld %lld\n",b,a);
    }
    return 0;
}

EIGHTS


Triple Fat Ladies

Link to the question : EIGHTS 

HINT :

No need to hard code. Just take out your calculator and check for the first few numbers whose cube ends with 888 and observe the pattern. 

Still cant get the pattern?
Here it is : 
1 - 192
2 - 442
3 - 692
4 - 942

RECOMMENDED  QUESTION :

Try solving this question.

SOURCE CODE :

#include<iostream>


using namespace std;

int main()

{

    long long k,t;

    cin>>t;

    while(t--)

    {

        cin>>k;

        cout<<192 + (k-1)*250<<endl;





    }

    return 0;

}

Wednesday, 22 July 2015

EGYPIZZA



Pizza

Link to the question : EGYPIZZA 

HINT :

Easy question. Just stick to the basics and code all the way.

RECOMMENDED QUESTION :

Try your hands on this question .

SOURCE CODE :



#include<iostream>

#include<cmath>

#include<cstdio>

using namespace std;

int main()

{

int n,i=0,ans=0;

int a1=0,a2=0,a3=0;

int n1,n2;

char op;

cin>>n;

for(i=0;i<n;i++)

{

cin >> n1 >> op >> n2;

if(n1==3) a3++;

else if(n2==2) a2++;

else if(n2==4) a1++;

}

ans=a3;

a1=a1-a3;

ans+=(a2-a2%2)/2;

if(a2%2)

{

ans++;

a1-=2;

}

if(a1 > 0) {

ans+=(a1-(a1%4))/4;

if(a1%4>0) {

ans++;

}

}

cout<<ans+1<<endl;

return 0;

}



    


DANGER


In Danger

Link to the question : DANGER 

HINT :

Josephus problem can be solved by using a circular queue but for k=2 we can derive a formula. First try solving by keeping number of people, n, equal to some power of 2, i.e, 4,8 etc. You will notice that the last to stay is at position 1. 
Hence we write  n = 2^m + t. Now, if t people are killed, the one with whom the cycle starts stays till the end. So, after killing t people, the cycle will start from the position 2*t - 1. Hence, you need to find that position.

RECOMMENDED QUESTION :

After getting an AC, try solving this question .

SOURCE CODE :

/* In Danger */

/* Sushant Gupta */



#include<stdio.h>

#include<math.h>

int main()

{

    char a[10];

    scanf("%s",a);



    while(1)

    {

        if(a[0]== '0' && a[1]== '0')

            return 0;



        long long int x;

        x = 10 * (a[0] - '0') + (a[1]- '0');

        x = x * pow(10, (a[3]- '0'));

        int m;

        m = log2(x);

        long long int n;

        n = pow(2,m);

        long long int ans;

        ans = 2*(x - n) + 1;

        printf("%lld\n",ans);

        scanf("%s",a);

    }



}

CUTCAKE


Eat all the brownies !

Link to the question : CUTCAKE 

HINT :

A good mathematical question. You can  approach to the solution of the question by thinking it this way - how will n lines cut a plane so that you get maximum pieces. Many of you might have solved this question during your entrance exam preparation. If not here's the solution:
1 line divides a plane into 2 parts.
2 lines divide it into 4 and similarly 3 lines divide it into 7.
If you go further, you will see that the differences of these numbers are in AP. Hence, derive the formula and solve it accordingly.

RECOMMENDED QUESTION :

You will love solving this adhoc question . 

SOURCE CODE :

#include<stdio.h>

#include<math.h>

int main()

{

      long long int n,tn;

      int t;

      scanf("%d",&t);

      while(t--)

      {

          scanf("%lld",&tn);

          if(tn==1)

            printf("0\n");



          else  {

          n= (1 + sqrt(1+ 8*(tn-1)));

          n=n/2;

          printf("%lld\n",n-1);

      } }

      return 0;



}

    

Tuesday, 21 July 2015

CRDS



Cards

Link to the question : CRDS 

HINT :

A very simple question. Just observe the pattern and derive a formula.

RECOMMENDED QUESTION :

Try this adhoc question .

SOURCE CODE :

#include<iostream>

using namespace std;

int main()

{

    long long t,n,s;

    cin>>t;



    while(t--)

    {

        cin>>n;

        s= (2*n*(n+1)/2) + (n*(n-1)/2);

        cout<<s%1000007<<endl;

    }



    return 0;

}

COMDIV

Number of common divisors

Link to the question : COMDIV 

HINT : 

The number of common divisors of two numbers is simply the number of divisors of their gcd.

RECOMMENDED QUESTION :

Try solving this question .

SOURCE CODE :

/* SPOJ - Number Of Common Divisors (COMDIV)
         - Sushant Gupta   */

#include<stdio.h>

int hcf(int n1, int n2)
{
    if(n2>n1)
        return hcf(n2,n1);
    else if (n2!=0)
       return hcf(n2, n1%n2);

    else
       return n1;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int a,b,c=0,i,h;
        scanf("%d%d",&a,&b);
        h= hcf(a,b);
        for(i=1;i*i<=h;i++)
        {
            if(h%i==0)
                c=c+2;

        }
        i=i-1;
        if(i*i==h)
            c--;
        printf("%d\n",c);

    }
    return 0;
}