暑假第一周 B

来源:互联网 发布:linux安装binwalk 编辑:程序博客网 时间:2024/06/05 01:53

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 longinteger type.

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

//B***********
#include<iostream>
#include<algorithm>
using namespace std;
long long gcd_(long long a, long long b){
  long long big = max(a, b);
  long long small = min(a, b);
  long long temp = big % small;
  return temp == 0 ? small : gcd_(small, temp);
}
int main(){
    long long n,a,b,p,q;
    while(cin>>n>>a>>b>>p>>q){
    long long s=0;
    s+=n/a*p;
    s+=n/b*q;
    long long tmp=a*b/(gcd_(a,b));
    long long tm=n/tmp;
    if(p>q){
        s-=tm*q;
    }
    else{
        s-=tm*p;
    }
    cout<<s<<endl;
    }
    return 0;
}

原创粉丝点击