Joty and Chocolate

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

Input
5 2 3 12 15
Output
39
Input
20 2 3 3 5
Output

51

题意:

有1到n个数,如果i%a==0,那么给p,如果i%b==0,那么给q,如果k%a==0||k%b==0,那么给p或者q,问你最多能给多少,显然肯定给p,q中大的那一个


#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
long long gcd(long long a,long long b)//貌似叫容斥原理,求a,b最大公约数
{
    if(b==0)
        return a;
    return gcd(b,a%b);
}
long long gbs(long long a,long long b)//求a,b最小公倍数,数学像我一样渣的自行查资料
{
    return a*b/gcd(a,b);
}
int main()
{
    int n,a,b;
    long long p,q,sum,num1,num2,num3;
    scanf("%d%d%d%I64d%I64d",&n,&a,&b,&p,&q);
    num1=n/a;
    num2=n/b;
    num3=n/gbs(a,b);
    printf("%I64d\n",(num1-num3)*p+(num2-num3)*q+num3*max(q,p));
    return 0;
}



猜你喜欢

转载自blog.csdn.net/Foverve1/article/details/52141744