Joty and Chocolate

探讨了通过特定算法解决如何最大化获取巧克力数量的问题。在一个由n个瓷砖组成的行中,根据瓷砖编号与给定数值的除法关系,选择最优颜色进行涂色以获得最多巧克力。

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值