Codeforces Joty and Chocolate

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

template<class T> T gcd(T a, T b)
{
    if(b==0)
    {
        return a;
    }

    else
    {
        return gcd(b, a%b);
    }
}

int main()
{
    LL n, a, b , p , q;
    while(cin >> n >> a >> b >> p >> q)
    {
        LL tile = (a / gcd(a,b)) * b; // lcm for n. if n=5 then, 1/2 or 1/3 + 2/2 + 3/3+4/2+5/2 or 5/3..so, lcm(a,b);

        LL total = ( (n / a) * p ) + ( (n / b) * q );

        tile = n / tile; // for n tile

        LL ans = total - ( tile * min(p,q) );

        cout << ans << "\n";
    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number