Here you will find solutions of many problems on spoj. If you want solution of some problem which is not listed in blog or have doubt regarding any spoj problem (which i have solved) or any programming concept (data structure) you can mail me @ raj.nishant360@gmail.com

And my humble request to you all that don't copy the code only try to understand the logic and algorithm behind the code. I have started this because if you tried as hard as you can and still can't find any solution to the problem then you can refer to this.
You can read my answer how to start competitive programming CLICK HERE

Monday, August 18, 2014

WPC5I - LCM Revisited

Here, is solution to the question WPC5I - LCM . If you still have a doubt let me know E-mail : surajjumpy@gmail.com

#include<bits/stdc++.h>
#define scan(x) scanf("%i",&x)

int prime[4900],prime_index = 1,number[47001]={0};

int main()
{
    int i,j,k,a,b,t,count1,count2,temp;    long long pro;

    prime[1] = 2;

    for(i = 3;    i <= 217;     i += 2)
    {
        if(number[i] == 0)
        {
            prime[++prime_index] = i;
            for(j = i*i, k = 2*i;    j <= 47000;    j += k)
                number[j] = 1;
        }
    }

    for(    ;   i <= 47000;  i += 2)
        if( number[i] == 0)
            prime[++prime_index] = i;

    scan(t);

    while(t--)
    {
        scan(a);
        scan(b);

        pro = i = 1;

        while(i <= prime_index && (a != 1 || b != 1))
        {
            count1 = count2 = 0;

            while(a%prime[i] == 0)
            {
                a /= prime[i];
                count1++;
            }

            while(b%prime[i] == 0)
            {
                b /= prime[i];
                count2++;
            }

            if(count1 != count2)
            {
                temp = (count1 > count2) ? count1 : count2;

                while(temp)
                {
                    pro *= prime[i];
                    temp--;
                }
            }

            i++;
        }

        if(a != b)
        {
            pro *= a;
            pro *= b;
        }

        printf("%lli\n",pro);
    }

    return 0;
}


No comments:

Post a Comment

Your comment is valuable to us