[关闭]
@xunuo 2017-02-22T11:19:23.000000Z 字数 1597 阅读 1203

Codeforces 678 C. Joty and Chocolate(求最大公约数和最小公倍数)


time limit per test 1 second memory limit per test 256 megabytes

数论


Dscription

    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.

Examples

input

5 2 3 12 15

output

39

input

20 2 3 3 5

output

51

题意:

有n块砖。他们的编号从1-n,如果i%a==0,则将这块砖涂为红色,他可以得到p个巧克力,如果i%b==0,则将这块砖涂为蓝色,他可以得到q个巧克力,问他最终可以得到多少个巧克力;

解题思路:

ans=n/a+n/b-(n/(max(a,b))*max(a,b));

PS

  1. ll gcd(ll a,ll b)
  2. {
  3. if(b==0)
  4. return a;
  5. return gcd(b,a%b);
  6. }

完整代码:

  1. //有1到n n个数,如果k%a==0,那么给p元,如果k%b==0,那么给q元
  2. //如果k%a==0&&k%b==0,那么给p元或者q元
  3. //问你最多给多少
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<math.h>
  7. #include<algorithm>
  8. using namespace std;
  9. #define ll long long
  10. ll gcd(ll a,ll b)
  11. {
  12. if(b==0)
  13. return a;
  14. return gcd(b,a%b);
  15. }
  16. int main()
  17. {
  18. ll n,a,b,p,q,sum;
  19. while(scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&p,&q)!=EOF)
  20. {
  21. sum=0;
  22. ll lcm=(a*b)/gcd(a,b);
  23. if(p>q)
  24. {
  25. sum+=(n/a)*p;
  26. sum+=(n/b)*q;
  27. sum-=(n/lcm)*q;
  28. }
  29. else
  30. {
  31. sum+=(n/b)*q;
  32. sum+=(n/a)*p;
  33. sum-=(n/lcm)*p;
  34. }
  35. printf("%lld\n",sum);
  36. }
  37. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注