题目:
Berland Football Cup starts really soon! Commentators from all over the world come to the event.
Organizers have already built n
commentary boxes. mregional delegations will come to the Cup. Every delegation should get the same number of the commentary boxes. If any box is left unoccupied then the delegations will be upset. So each box should be occupied by exactly one delegation.
If n
is not divisible by m, it is impossible to distribute the boxes to the delegations at the moment.
Organizers can build a new commentary box paying a
burles and demolish a commentary box paying bburles. They can both build and demolish boxes arbitrary number of times (each time paying a corresponding fee). It is allowed to demolish all the existing boxes.
What is the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by m
)?
InputThe only line contains four integer numbers n
, m, a and b ( 1≤n,m≤1012, 1≤a,b≤100), where n is the initial number of the commentary boxes, m is the number of delegations to come, a is the fee to build a box and bis the fee to demolish a box.
OutputOutput the minimal amount of burles organizers should pay to satisfy all the delegations (i.e. to make the number of the boxes be divisible by m
). It is allowed that the final number of the boxes is equal to 0.
Examples9 7 3 8
15
2 7 3 7
14
30 6 17 19
0
In the first example organizers can build 5
boxes to make the total of 14 paying 3burles for the each of them.
In the second example organizers can demolish 2
boxes to make the total of 0 paying 7burles for the each of them.
In the third example organizers are already able to distribute all the boxes equally among the delegations, each one get 5
boxes.
思路:
这个题大意是给m个队伍分相等数量的房子,现在有n个房子,建一个房子是a元,拆房子是b元。求出最小值。
源码:
#include
using namespace std;
int main()
{
long long n, m, a, b, min,jian;
cin >> n >> m >> a >> b;
if (n%m == 0)cout << "0" << endl;
else
{
min = (n%m)*b;
jian= ((n/m+1)*m-n)*a;
if (jian <= min)min = jian;
cout << min << endl;
}
return 0;
}