codeforces 678C Joty and Chocolate

来源:互联网 发布:js中换行符\n和br 编辑:程序博客网 时间:2024/05/18 03:09

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

这个题挺简单的,给你n,a,b,p,q;

给你1道n的瓷砖,如果瓷砖能被a整除,就可以涂红色,如果能被b整除就能涂蓝色,如果涂成红色每块瓷砖可以得到p块巧克力,如果涂成蓝色,每块可以得到q块巧克力

问你能得到最大的巧克力的块数是多少

首先如果都涂成红色,可以得到多少巧克力

然后,都涂成蓝色,可以的到多少巧克力

然后加起来

最后算一下这些数字中有几个同时可以被a和b整除的,就分类讨论一下

如果p大的话,就把蓝色的公共部分擦去

否则擦去红色的公共部分

#include<iostream>#include<cstdio>#include<cstring>#include<cstdlib>#include<cmath>#include<string>#include<vector>#include<stack>#include<set>#include<map>#include<queue>#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;long long ans=0;scanf("%lld %lld %lld %lld %lld",&n,&a,&b,&p,&q);ans+=n/a*p;ans+=n/b*q;long long tmp=a*b/(gcd_(a,b));long long tm=n/tmp;if(p>q){ans-=tm*q;}else{ans-=tm*p;}printf("%lld\n",ans);    return 0;}


0 0