1. 程式人生 > >【CodeForces 990A】Commentary Boxes

【CodeForces 990A】Commentary Boxes

cup end demo ini IT col ons 題目 www

題目鏈接

luogu & CodeForces

題目描述

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 }

【CodeForces 990A】Commentary Boxes