codeforces 678C. Joty and Chocolate(容斥)

来源:互联网 发布:get it ? 编辑:程序博客网 时间:2024/05/18 01:35
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 byb. 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

题目的意思是给你1-n,n个数,你可以把a的倍数染成红色获得p价值,把b的倍数染成蓝色获得q价值,a,b公倍数任意一种颜色问最大价值

直接把a的倍数的和b的倍数的价值全加起来,公倍数染价值大的,即减去小的价值的即可


#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<queue>#include<vector>#include<algorithm>using namespace std;#define inf 0x3f3f3f3flong long gcd(long long x,long long y){    if(y==0) return x;    else return(gcd(y,x%y));}long long lcm(long long x,long long y){    return x*y/gcd(x,y);}int main(){long long n,a,b,p,q;    while(~scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&p,&q))    {        long long lm=lcm(a,b);        printf("%lld\n",(n/a)*p+(n/b)*q-min(p,q)*(n/(lm)));    }    return 0;}


0 0
原创粉丝点击