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.
题目大意一共有n块砖要涂,如果是a的倍数涂红色,b的倍数涂蓝色。一块红色价值p,一块蓝色价值q。难点在于既是a的倍数也是b的倍数,如果用循环暴力则会超时,本题利用辗转相除法,算出最小公倍数;
然后用n除以最小公倍数即可以得既是a的倍数又是b的倍数的数目。
#include <iostream> using namespace std; int main() { long long int n,a,b,p,q,sum=0,x,y,z,a1,b1,c; cin>>n>>a>>b>>p>>q; x=n/a; y=n/b; a1=a; b1=b; while(b1!=0) { c=a1%b1; a1=b1; b1=c; } z=(n*a1)/(a*b); if(p>q) { sum=p*x+q*(y-z); } else { sum=q*y+p*(x-z); } cout<<sum<<endl; }