Tuesday, June 21, 2016

10892 - LCM Cardinality

Problem Link : https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1833

Solution Idea :Easy lcm problem , where need to store all divisors of the n (no given) and then exploit bruteforce to find lcm of which pair of the divisors is equal to n (number given).

Solution :
#include<bits/stdc++.h>
#define ll long long
using namespace std;

vector<int>divisor;

void nod(ll int n)
{
    ll int sq=sqrt(n);
    ll int c=0;
     ll int m=0;

    for(ll int i=1;i<=sq;i++)
    {

        if(n%i==0)
        {
            c=n/i;
            divisor.push_back(i);
            if(c!=i) divisor.push_back(c);
        }
    }

}

ll int lcm(ll int p,ll int q)
{
     ll int c;
   c=p*q;
    ll int gcd,lcm;

    ll int temp;
    while(q!=0){

        temp=q;
        q=p%q;
        p=temp;
    }

    gcd=p;
    lcm=c/gcd;
    return lcm;

}

int main()
{

    ll int n,m,cnt=0,s;
    while(scanf("%lld",&n) and n)
    {
        nod(n);
        cnt=0;
         s=divisor.size();
        for(ll int i=0;i<s;i++)
        {
            for(ll int j=i;j<s;j++)
            {
                if(lcm(divisor[i],divisor[j])==n) cnt++;
            }
        }
        printf("%lld %lld\n",n,cnt);
        divisor.clear();
    }

}

No comments:

Post a Comment