Showing posts with label lcm. Show all posts
Showing posts with label lcm. Show all posts

Monday, 27 July 2015

GUESSTHE

Guess the Number

Link to the question : GUESSTHE 

HINT :

We need to find the LCM of the numbers which are given a Y, and then check for the numbers which are given N, whether they divide the LCM.

RECOMMENNDED QUESTION:

You would surely love solving this question  after  trying your hands in this one.

SOURCE CODE :

#include<stdio.h>
long long int gcd(long long int a,long long int b)
{
    if(b>a)
        return gcd(b,a);
    else if(b==0)
        return a;
    else
        return gcd(b,a%b);
}
long long int lcm(long long int a,long long int b)
{
    return a*b/gcd(a,b);
}
int main()
{
    char c,a[22];

    while(1) {
            int i=1,j=0;
            long long int k=1;
            scanf("%c",&c);
            while(c!='\n'&&c!='*')
           {



                      if(c=='Y')
                          k= lcm(k,i);
                     else if(c=='N')
                           a[j++]= i;
                     i++;
                     scanf("%c",&c);


           }
           if(c=='*')
            return 0;
            else {
           for(i=0;i<j;i++)
           {
               if(k%a[i]==0)
               {
                   k=-1;
                   break;
               }
           }
           printf("%lld\n",k);
            }
    }

}
 

Wednesday, 15 July 2015

ANARC09B


Tiles of Tetris, Not!

Link to the question : ANARC09B 

HINT :

A very simple question. By looking at the example or by just getting the feel of the question you will understand that all you need to find is the least common multiple of the two numbers.

RECOMMENDED QUESTION :

Try this p&c question after this one.

SOLUTION :

 #include<iostream>

 using namespace std;

int main()

{



    long long  a=1,b=1,g;



    while(a!=0 && b!=0)

    {

        cin>>a>>b;

        if(a!=0 && b!=0) {

        long long i;

        while(a%2==0 && b%2==0)

        {

            a=a/2;

            b=b/2;

        }

        long long

         s;

        if(a<b)

            s=a;

        else s=b;

        for(i=3;i<=s;i=i+2)

        {

            while(a%i==0 && b%i==0)

            {

                a=a/i;

                b=b/i;

            }

        }

        cout<<a*b<<endl;

    } }

    return 0;



}