678C. Joty and Chocolate - Codeforces Solution C++

  Problem Link : 678C. Joty and Chocolate 


✅ C++ Solution :

 
#include<bits/stdc++.h>
using namespace std;

#define ll long long int

ll gcd(ll a, ll b)
{
	if(b==0)
		return a;
	return gcd(b,a%b);
}

ll lcm(ll a, ll b)
{
	return (a/gcd(a,b))*b;
}

int main()
{
	ll n,a,b,p,q;
	cin>>n>>a>>b>>p>>q;

	ll ac,bc,mc,t;
	ac=n/a;
	bc=n/b;
	
	t=lcm(a,b);
	mc=n/t;
    if(mc==0)
        cout<<ac*p+bc*q;
	else
	{
		if(q>p)
			cout<<(ac-mc)*p+bc*q;
		else
			cout<<ac*p+(bc-mc)*q;
	}
}

 

Thank you for your patience reading. If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Whatsapp or Facebook. 

😇Happy Learning!!