[关闭]
@Chilling 2017-01-15T17:14:55.000000Z 字数 1384 阅读 1254

CodeForces-678C: Joty and Chocolate(LCM)

数论


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 .

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

5 2 3 12 15
20 2 3 3 5

Sample Output

39
51

题意:输入n,a,b,p,q,从1到n中找到a的倍数,b的倍数,a和b共同的倍数。a的倍数个数乘以p,b倍数个数乘以q,共同倍数的个数乘以p和q中的最大值,得到结果。

分析: a和b的倍数个数就是用n除以他们取整,当中可能有重复的,就是a和b共同的倍数。先找到a和b的最小公倍数,n除以它得到共同倍数的个数,ab倍数个数减去共同倍数的个数= =好绕


  1. #include<stdio.h>
  2. #include<algorithm>
  3. #define LL long long
  4. using namespace std;
  5. LL gcd(LL a,LL b) //辗转相除法求最大公因数
  6. {
  7. LL t;
  8. while(b)
  9. {
  10. t=b;
  11. b=a%b;
  12. a=t;
  13. }
  14. return a;
  15. }
  16. int main()
  17. {
  18. LL n,a,b,p,q,lcm;
  19. LL x1,x2,x3;
  20. while(scanf("%lld%lld%lld%lld%lld",&n,&a,&b,&p,&q)!=EOF)
  21. {
  22. x1=n/a;
  23. x2=n/b;
  24. lcm=a*b/gcd(a,b);
  25. x3=n/lcm;
  26. x1-=x3;
  27. x2-=x3;
  28. printf("%lld\n",x1*p+x2*q+x3*max(p,q));
  29. }
  30. return 0;
  31. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注