Educational Codeforces Round 13 C. Joty and Chocolate 水题

时间:2023-12-19 10:06:56

C. Joty and Chocolate

题目连接:

http://www.codeforces.com/contest/678/problem/C

Description

Little Joty has got a task to do. She has a line of n tiles indexed from 1 to n. She has to paint them in a strange pattern.

An unpainted tile should be painted Red if it's index is divisible by a and an unpainted tile should be painted Blue if it's index is divisible by b. So the tile with the number divisible by a and b can be either painted Red or Blue.

After her painting is done, she will get p chocolates for each tile that is painted Red and q chocolates for each tile that is painted Blue.

Note that she can paint tiles in any order she wants.

Given the required information, find the maximum number of chocolates Joty can get.

Input

The only line contains five integers n, a, b, p and q (1 ≤ n, a, b, p, q ≤ 109).

Output

Print the only integer s — the maximum number of chocolates Joty can get.

Note that the answer can be too large, so you should use 64-bit integer type to store it. In C++ you can use the long long integer type and in Java you can use long integer type.

Sample Input

5 2 3 12 15

Sample Output

39

Hint

题意

有1到n n个数,如果k%a0,那么给p元,如果k%b0,那么给q元

如果k%a0||k%b0,那么给p元或者q元

问你最多给多少

题解:

k%a0||k%b0 > k%lcm(a,b)0 给max(p,q)元

然后搞一波就好了……

代码

#include<bits/stdc++.h>
using namespace std; long long gcd(long long a,long long b)
{
if(b==0)return a;
return gcd(b,a%b);
}
long long lcm(long long a,long long b)
{
return a*b/gcd(a,b);
}
long long n,a,b,p,q;
int main()
{
cin>>n>>a>>b>>p>>q;
long long ans1 = n/a;
long long ans2 = n/b;
long long ans3 = n/lcm(a,b);
ans1-=ans3,ans2-=ans3;
cout<<ans1*p+ans2*q+(ans3)*max(p,q)<<endl;
}