C. Joty and Chocolate
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.
The only line contains five integers n, a, b, p and q (1 ≤ n, a, b, p, q ≤ 109).
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.
5 2 3 12 15
39
20 2 3 3 5
51
小析:有(1~n)块瓷砖,下标可以被a或b整除可以被涂上对应颜色,每种颜色获得的巧克力数不同,若可被a and b 整除,可以选择一种颜色涂抹。要求获得最多的巧克力数
这道题有点类似容斥定理吧
1 #include <bits/stdc++.h> 2 #include <string.h> 3 #include <algorithm> 4 #include <stdio.h> 5 #include <cstdlib> 6 7 8 using namespace std; 9 typedef long long LL; 10 LL gcd(LL a,LL b){ 11 return a%b==0?b:gcd(b,a%b); 12 } 13 14 int main(){ 15 LL n,a,b,p,q; 16 cin>>n>>a>>b>>p>>q; 17 LL lcm=a/gcd(a,b)*b; 18 LL x=n/a,y=n/b,z=n/lcm; 19 LL sum=x*p+y*q; 20 if(z) 21 sum=(p>q)?sum-z*q:sum-z*p; 22 cout<<sum<<endl; 23 }