990A Commentary Boxes

A. Commentary Boxes
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Berland Football Cup starts really soon! Commentators from all over the world come to the event.

Organizers have already built nn commentary boxes. mm regional 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 nn is not divisible by mm, it is impossible to distribute the boxes to the delegations at the moment.

Organizers can build a new commentary box paying aa burles and demolish a commentary box paying bb burles. 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 mm)?

Input

The only line contains four integer numbers nnmmaa and bb (1n,m10121≤n,m≤10121a,b1001≤a,b≤100), where nn is the initial number of the commentary boxes, mm is the number of delegations to come, aa is the fee to build a box and bb is the fee to demolish a box.

Output

Output 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 mm). It is allowed that the final number of the boxes is equal to 00.

Examples
input
Copy
9 7 3 8
output
Copy
15
input
Copy
2 7 3 7
output
Copy
14
input
Copy
30 6 17 19
output
Copy
0
Note

In the first example organizers can build 55 boxes to make the total of 1414 paying 33 burles for the each of them.

In the second example organizers can demolish 22 boxes to make the total of 00 paying 77 burles 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 55 boxes.



题意:足球杯组织者已经只做了n个箱子,有m个代表团回去参加,让每个代表团都有箱子,n必须整除m。现在可以选择制造箱子或者拆掉箱子,制造箱子成本a,,拆掉箱子成本b。让你用最小的花费,满足题意。但是样例二就就奇怪了,或者就是我翻译错了。箱子可以全部拆掉为0....

题解:模拟  如果n是m的倍数,就直接输出0,n<m,就判断全部拆完和增加到m的个数花费取小的花费,n>m就判断需要增加的箱子花费和减少箱子的花费取小的。其实还可在优化代码,总结规律:m-n%m*a和n%m*b取小的代码就更短了。


#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,m,a,b,ans;
    cin>>n>>m>>a>>b;
    if(n%m==0) ans=0;
    else if(n<m)  ans=min(n*b,(m-n)*a);
    else
    {
        long long y=n%m,z=n/m+1;
        ans=min((z*m-n)*a,y*b);
    }
    cout<<ans<<endl;
    return 0;
}

优化代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long n,m,a,b;
    cin>>n>>m>>a>>b;
    cout<<min((m-n%m)*a,n%m*b)<<endl;
    return 0;
}

python:

n,m,a,b=map(int,input().split())
print(min((m-n%m)*a,n%m*b))

猜你喜欢

转载自blog.csdn.net/memory_qianxiao/article/details/80657632
今日推荐