Educational Codeforces Round 13 C Joty and Chocolate(数学)

思路:有1到n n个数,如果k%a==0,那么给p元,如果k%b==0,那么给q元,如果k%a==0||k%b==0,那么给p元或者q元,问你最多给多少,显然猜一下k%lcm(a,b)=0给max(p,q)就好了


#include
using namespace std;
#define LL long long
LL gcd(LL a,LL b)
{
	if(b==0)
		return a;
	return gcd(b,a%b);
}
LL lcm(LL a,LL b)
{
	return a*b/gcd(a,b);
}

int main()
{
    LL n,a,b,p,q;
	cin >> n >> a >> b >> p >> q;
	LL ans1 = n/a;
	LL ans2 = n/b;
	LL ans3 = n/lcm(a,b);
	ans1-=ans3;
	ans2-=ans3;
	cout << ans1*p+ans2*q+ans3*max(p,q) << endl;
}

C. Joty and Chocolate
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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 nabp 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.

Examples
input
5 2 3 12 15
output
39
input
20 2 3 3 5
output
51


你可能感兴趣的:(数论及数学)