Showing posts with label mathematics. Show all posts
Showing posts with label mathematics. Show all posts

Tuesday, 8 September 2015

MOHIB



Mohib and series

 Link to the question : MOHIB

HINT :

Given x and the avg as input, the number of elements in the sequence, n, and the sum of the sequence can be very easily calculated by using the formula - sum of seq. / n = avg. 
Now, to find the largest possible number, lets say M, (sum of sequence - M) should be minimum. Hence in order for that to be minimum, the sequence excluding M should be 1,2,3,...n-1 such that its sum is minimum.

RECOMMENDED QUESTION :

After getting AC in this one, try getting AC in this question .

SOURCE CODE :

#include<stdio.h>

int main()
{
    int t;
    scanf("%d",&t);  
    while(t--)
    {
         int x,avg,n;
         scanf("%d%d",&x,&avg);
         n = avg - x;
         int sum = n * (avg + 1);
         sum = sum - (n*(n-1)/2) ;

         printf("%d\n",sum);
    }
    return 0;
}              

Sunday, 26 July 2015

GIRLSNBS

Girls and Boys

Link to the question : GIRLSNBS 

HINT :

Let g be the number of girls and b be the number of boys and let b>g. Now, if * represents the girls and let their arrangement be like :
      *     *      *      *        *   , then think how many boys will you fill on those spaces so that you get the minimum gender regularity.There are g+1 spaces. So first we fit all the spaces with equal number of boys and then distribute the remaining boys to one boy per space.

RECOMMENDED QUESTION :

I would like my readers to solve this question after trying this one.

SOURCE  CODE :

#include<stdio.h>

int main()

{

    int g,b,x=1,y=1;

    while(x!= -1 && y!=-1)

    {

                scanf("%d%d",&g,&b);

                x=g; y=b;

                if(x!=-1 && y!=-1)

                {

                    if(g==b){

                            if(g==0)

                               printf("0\n");

                       else

                        printf("1\n");

                    }

                    else

                    {

                        if(g>b)

                        {

                            x=g; y=b;

                        }

                        else{ x=b; y=g;}

                        if((x%(y+1))==0)

                            printf("%d\n",(x/(y+1)));



                        else

                            printf("%d\n",(x/(y+1))+1);

                    }

                }

    }

    return 0;

}
    

Saturday, 25 July 2015

FUNPROB

Yanu in Movie theatre

Link to the question : FUNPROB 

RECOMMENDED QUESTION :

Try solving this question after solving this.

SOURCE CODE :

#include<stdio.h>
int main()
{
     int m=1,n=1;
     double x,y;
     while(m!=0 && n!=0)
     {
         scanf("%d%d",&n,&m);
         if(n!=0 && m!=0)
         {
             if(n>m || m==0)
                printf("0.000000\n");
             else if(n==0)
                printf("1.000000\n");
             else
             {
                 x=m;
                 y=n;
                 printf("%.6lf\n",(x-y+1.0)/(x+1.0));
             }

         }
     }
     return 0;
}

FENCE1

Build a Fence

Link to the question : FENCE1 

HINT : 

Imagine a very long straight ruler and a piece of smallish rope. The rope is tied to the ruler at one end and loose at the other. Now connect the other end of the rope to the ruler and you'd get a shape with a straight bottom (made of the ruler itself) and a curvy top (made by the rope). This shape has an area, what is its maximum area given the length of the rope?

RECOMMENDED QUESTION:

Try your hands in this question after getting an AC in this.

SOURCE CODE :

#include<stdio.h>

int main()
{
    int l,x=1;
    double s;
    while(x!=0)
    {
        scanf("%d",&l);
        x=l;
        if(x!=0)
        {
            s= (float)(l*l/(2*3.1415926 ));
            printf("%.2f\n",s);
        }
    }



    return 0;
}

FCTRL2

Small factorials

Link to the quesstion : FCTRL2 

HINT :

Since factorials of numbers like 100 will be very long, almost 160 digits. So we need to store the result in an array. Check the code on how to implement it or you can also read its tutorial in codechef.

RECOMMENDED QUESTION :

Try solving this question .

SOURCE CODE :


#include<stdio.h>

#include<stdlib.h>



void fact(int num,int a[],int n)

{

     int temp=0,x=0,i=0,j,k;

     for(j=num-1;j>0;j--)

     {    i=0;

         for(k=n;k>0;k--)

        {



              x= a[i]*j + temp;

              a[i]= x%10;

              temp= x/10;



               i++;





        }



        while(temp!=0)

        {

           a[i]=temp%10;

          temp=temp/10;



           i++;

           n=i;

        }



     }



     for(j=n-1;j>=0;j--)

        printf("%d",a[j]);

        printf("\n");



}



int main()

{

    int t;

    scanf("%d",&t);

    while(t--)

   {   int a[200]={0};

       int n;

       scanf("%d",&n);

       int n2=n; int i=0;

       while(n>0)

       {

           a[i]=n%10;

           n=n/10;



           i=i+1;

       }

       int k=i;



       fact(n2,a,k);

   }

   return 0;





}

Friday, 17 July 2015

AP3

AP - Complete The Series v2

 Link to the question : AP3

HINT :

Again a simple question based on the arithmetic progression. The problem becomes tricky if not taken care of the precisions.

RECOMMENDED QUESTION :

Try your hands on this question .

SOURCE CODE :

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

int main()
{
    int t; scanf("%d",&t);
    while(t--)
    {
       long long int a3,a3l,s,i,a,d,len; 
          
      long double n,sq,diff;
      scanf("%lld",&a3);
      scanf("%lld",&a3l);
      scanf("%lld",&s);
     
      sq=sqrtl(((5.0*a3l+7.0*a3+2.0*s)*(5.0*a3l+7.0*a3+2.0*s))-(48.0*(a3l+a3)*s));
      n=((5.0*a3l+7.0*a3+2.0*s)+sq)/(2.0*(a3l+a3));
     
      len=llrintl(n);
      printf("%lld\n",len);
     
      diff=(a3l-a3)/(len-6.0);
      d=llrintl(diff);
     
      a=(a3-(2*d));
       for(i=0;i<len;i++)
       {
                       printf("%lld ",a+i*d);
       }
        printf("\n");
      
      
       }  
     
     return 0;
}