题目链接
题目描述
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 )?
中文翻译(摘自洛谷)
对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价
输入:
n,m,a,b
输出:
最小的代价
感谢@zhaotiensn 提供翻译
解题思路
额,没啥说的。
枚举向上取还是向下取,比较一下就好了。
还有,别忘了long long!!!
代码
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 int main(){ 6 long long n,m,a,b; 7 cin>>n>>m>>a>>b; 8 long long x=n/m; 9 long long add=((x+1)*m-n)*a; 10 long long jian=(n-x*m)*b; 11 cout<<min(add,jian)<<endl; 12 }
原文地址:https://www.cnblogs.com/Fang-Hao/p/9248815.html