Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Monday, 5 October 2015

TWOSQRS


Two squares or not two squares

Link to the question : TWOSQRS

HINT :

Logic seems very clear. Just we need check if the number can be represented as a sum of two squares or not. A little bit optimisation may be preferrable.

RECOMMENDED QUESTION :

Try solving this  question .

SOURCE CODE :

#include<stdio.h>

#include<math.h>

void twosq(long long int x)

{

    long long int i,j=0;

    i= sqrt(x);

    while(i>0) {

    if(j*j>x)

      {



        printf("No\n");

         break;

      }

    else if(i*i + j*j == x)

        {



         printf("Yes\n");

         break;

        }

    else if(i*i + j*j <x)

         j++;

    else

        i--;

    }





}

int main()

{

    int t;

    scanf("%d",&t);

    while(t--)

    {

        long long int n;

        scanf("%lld",&n);

        twosq(n);

    }

    return 0;



}

Saturday, 25 July 2015

FCTRL


Factorial

Link to the question : FCTRL 

HINT :

For finding the number of trailing zeroes, we need to find how many factors of 10 are present. Since 10 = 2 *5 , we basically need to find the number of factors of 5, since its obvious that factors of 2 will be greater in number.

RECOMMENDED QUESTION :

Try this question afer completing this.

SOURCE CODE :


#include<stdio.h>

    #include<math.h>



    int main()

    {

      int t,c,i;

      long n;

      scanf("%d",&t);

      while(t--)

      {

          c=0;i=1;

          scanf("%ld",&n);

          while(n/pow(5,i)>=1)

          {

              c=c+(int)(n/pow(5,i));

              i=i+1;

          }

          printf("%d\n",c);

      }

       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;
}

Wednesday, 22 July 2015

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

CEQU


Crucial Equation

Link to the question : CEQU 

HINT : 

We need to find whether there exists an integer solution for x and y  which satisfy the equation ax + by = c. This can be done by finding the gcd of a and b and checking if it divides c.

RECOMMENDED QUESTION :

I think you will love solving a dp question after solving this one. So try your hands on this question .

SOURCE CODE :

#include<stdio.h>

gcd(int m,int n){

 if(n==0)

  return m;

 else 

  return gcd(n,m%n);

}

int main(){

 int a,b,c,t,g,e=1;

 scanf("%d",&t);

 while(t--){

  scanf("%d %d %d",&a,&b,&c);

  g=gcd(abs(a),abs(b));

  if(c%g==0)

   printf("Case %d: Yes\n",e);

  else 

   printf("Case %d: No\n",e);

   e++;

 }

 return 0;

}



Wednesday, 15 July 2015

AP2


AP - Complete The Series (Easy)

Link to the question : AP2 

HINT : 

Simply apply the formulae that you have learnt in your elementary school. A very easy question on arithmetic progression , even the question states it. But dont forget to simplify the equations a bit if you dont want your code to be  a bit messy.

RECOMMENDED QUESTION :

Try this gcd question .

SOURCE CODE :

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

Tuesday, 30 June 2015

COMDIV


Number Of Common Divisors

Link to the question: COMDIV

HOW TO APPROACH:

The problem asks us to find the number of common divisors between two numbers. So its very obvious that the number which is formed by the common divisors of both the numbers is their gcd.

  RECOMMENDED QUESTION :

Try this sorting question after this one.

 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;

}