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;
}
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
Post a Comment