Showing posts with label pattern. Show all posts
Showing posts with label pattern. Show all posts

Saturday, 5 September 2015

MKEQUAL

Make them equal !

Link to the question : MKEQUAL 

HINTS :

Very simple problem. Pen paper work. Try out with any n numbers such tha sum of n numbers is not divisible ny n and again with n numbers such that this time its divisible. You will definitely get the logic.

SOURCE CODE :


#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int a[n],i;
        long long int s=0;
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            s=s+a[i];
        }
        if(s%n==0)
            cout<<n<<endl;
        else
            cout<<n-1<<endl;

    }

    return 0;
}

Wednesday, 12 August 2015

MANGOES


Real Mangoes for Ranjith

Link to the question : MANGOES 

HINT :

The question might look lengthy and you may think implementing the algorithm will  be a tough task, but a little careful observation may make this question look very simple. As per the question mangoes are real if GCD of each pair of the  set  {mi, mi+1, mi+2} equals 1. Now this is only possible when mi and (mi + 2) are odd else if they are even, they will have at least 2 as their GCD. Hence all you need to is find the sum of the odd terms.

SOURCE CODE :

 #include<stdio.h>
int main()
{
    int t;
    long long int n,x,s;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&n);
        x=n;
        if(n%2==0)
            n=(n-2)/2;
        else
            n=(n-1)/2;

        s=(n*n)%x;
        printf("%lld\n",s);

    }
    return 0;
}
 

MAIN74


Euclids algorithm revisited

Link to the question : MAIN74 

HINT :

The numbers form the fibonacci sequence if you try solving it for some test cases.
Example for n= 2 loops the answer will be 5 (3,2).
              for n= 3 loops, result is 8 (5,3).
              for n=4 loops, output will be 13 (8,5). 
And so on...
Hence it is obivious that for n >=2 output is (n + 3)th fibonacci term.
Now to solve the problem in O(logn) time you should use the optimized matrix multiplication method to generate the fibonacci term.

SOURCE CODE :

#include<stdio.h>

long long MAX=1000000007;

void multiply(long long F[2][2],long long M[2][2]);
void power(long long F[2][2],long long n);
long long fib(long long n);

long long fib(long long n)
{
    long long F[2][2]={{1,1},{1,0}};
    if(n==0)
        return 0;
    power(F,n-1);
    return F[0][0];
}

void multiply(long long F[2][2],long long M[2][2])
{

    long long x = ((F[0][0]*M[0][0])%MAX + (F[0][1]*M[1][0])%MAX)%MAX;
    long long y =  ((F[0][0]*M[0][1])%MAX + (F[0][1]*M[1][1])%MAX)%MAX;
    long long z =  ((F[1][0]*M[0][0])%MAX + (F[1][1]*M[1][0])%MAX)%MAX;
    long long w =  ((F[1][0]*M[0][1])%MAX + (F[1][1]*M[1][1])%MAX)%MAX;

    F[0][0] = x;
    F[0][1] = y;
    F[1][0] = z;
    F[1][1] = w;

}

void power(long long F[2][2], long long n)
{
    if( n == 0 || n == 1)
        return;
    long long M[2][2] = {{1,1},{1,0}};

    power(F, n/2);
    multiply(F, F);

    if( n&1)
    multiply(F, M);
}

int main()
{
    int t;
    long long int n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld", &n);
        if(n==0)printf("0\n");
        else if(n==1)printf("2\n");
        else printf("%lld\n",(fib(n+3))%MAX);
    }

    return 0;
}

Friday, 7 August 2015

LASTDIG

The last digit

Link to the question : LASTDIG 

HINT :

No need to worry about the constraints. Take out a pen and paper and notice the pattern of last digit for the powers of number 1 to 9. And then derive a general formula to shorten your code as the source limit is very small.

SOURCE CODE :


#include <stdio.h>
  int main(void) {
long long int a,p,q,b;
int t;
scanf("%d",&t);
while(t--)   {
scanf("%lld %lld",&a,&b);
p=a%10;
q=b%4;
if(b==0)
    printf("1\n");
else if(p==1||p==0||p==5||p==6)
printf("%d\n",p);
else if(q==1)
    printf("%d\n",p);
else if(q==2)
    printf("%d\n",((p*p)%10));
else if(q==3)
    printf("%d\n",((p*p*p)%10));
else if(q==0)
    printf("%d\n",((p*p*p*p)%10));
}
return 0;
}

Thursday, 6 August 2015

IITKWPCB

Check the coprimeness

Link to the question : IITKWPCB 

HINT :

Observe the test cases and derive the formula.

SOURCE CODE :

#include <stdio.h>
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
    long long int n,c;
    scanf("%lld",&n);
    if(n%2==0)
    {
        c=n/2 - 1;
        if(c%2==0)
            printf("%lld\n",c-1);
        else
            printf("%lld\n",c);
    }
    else
    {
        printf("%lld\n",n/2);
    }
}
return 0;
}
 

HUBULLU

Link to the question : HUBULLU

HINT :

Observe carefully. Try some simple cases by taking n a  small number and then you will get the answer.

SOURCE CODE :


/* Hubulullu */
/* sushant gupta */

#include<iostream>

using namespace std;
int main()
{
     int t,s;
     long long int n;
     cin>>t;
     while(t--)
     {
         cin>>n>>s;
         if(s==0)
            cout<<"Airborne wins."<<endl;
         else
            cout<<"Pagfloyd wins."<<endl;
     }
     return 0;
}

Wednesday, 29 July 2015

HC


Happy Coins

Link to the question : HC

HINT :

You can ignore the word consecutive. Though I wont say it misleading, but not taking that into consideration will make the problem solving a bit more simple. Try some cases and even if you cant check the source code.

SOURCE CODE :


#include<stdio.h>

#include<string.h>

int main()

{

long int t,i,n,count=0;

char s[4],a[]="lxh";

scanf("%ld",&t);

while(t--)

{

    count=0;

    scanf("%ld",&n);

    while(n--)

    {

        scanf("%s",s);

        if(strcmp(s,a)==0)

            count++;

    }

    if(count%2==0)

        printf("hhb\n");

    else

        printf("lxh\n");



}

return 0;



}

RECOMMENDED QUESTION :

After solving this question, I would like you to try your hands out in this question . 

Saturday, 18 July 2015

BOMARBLE

D - Playing with Marbles

Link to the question : BOMARBLE 

HINT :

Simple formula to get accepted. We need to solve it using recursion. Observe the sample input and the output and try to get a recursive relation among them. If still stuck, check the source code.

RECOMMENDED QUESTION :

Try your hands on this ad-hoc question .

SOURCE CODE :

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

long long int rec(int m)
{
    if(m==1)
        return 5;
    else if(m==2)
        return 12;
    else
        return rec(m-1) + 10 + (m-3)*3;
}

int main()
{
    int n,x=1;;
    long long int s;


    while(x!=0)
    {
           scanf("%d",&n);
           x=n;
           if(x!=0)
           {
               s = rec(x);
               printf("%lld\n",s);
           }


    }
    return 0;

}

BEENUMS


Beehive Numbers

Link to the question : BEENUMS 

HINT :

The question may be a bit lengthy and also it might be difficult for us to imagine a beehive following the norms as per the question. But we dont have to worry about that. All we need to observe is the pattern in given example and VOILA our question is solved. 


RECOMMENDED QUESTION :

Try your hands in this question.

SOURCE CODE :

#include<stdio.h>
#include<math.h>
int main()
{
    long long int n,x=1,y;
    double t;
    while(x!=-1)
    {
        scanf("%lld",&n);
        x=n;
        if(x!=-1)
        {
            if(n%6==1)
            {
                  t= sqrt(1+ (4*(n-1)/3));
                  y= (int)(t*10);
                  if(y==t*10)
                    printf("Y\n");
                  else
                    printf("N\n");
            }
            else
            printf("N\n");
        }
    }
    return 0;
}

Wednesday, 1 July 2015

ABSP1

Abs(a-b) I

Link to the question: ABSP1 

HINT :

You must have already noticed that the number are in sorted order. If you do some rough work for input of 5-6 numbers and form pairs as given in the question and subtract you will notice that each element in the i-th position is added i-th times and subtracted n-1-i th times. Using this you can write your code.

SOURCE CODE :

/* ABSP1 */ /* Sushant Gupta */
#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i;
        scanf("%d",&n);
        long long int a[n],s=0;
        for(i=0;i<n;i++)
            scanf("%lld",&a[i]);
        for(i=n-1;i>=0;i--)
            s= s+ (i*a[i]) - a[i]*(n-1-i);
        printf("%lld\n",s);
    }
    return 0;
}