CF 990A. Commentary Boxes【数学/模拟】

【链接】:CF
【题意】:对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价。
【分析】:分情况讨论,自己多举几个栗子。
【代码】:

#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
using namespace std;

typedef long long ll;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const ll LNF = 1e18;
const int maxn = 1e3 + 100;
const int maxm = 100;
const double PI = acos(-1.0);
const double eps = 1e-8;
//const int dx[] = {-1,1,0,0,1,1,-1,-1};
//const int dy[] = {0,0,1,-1,1,-1,1,-1};
int dx[] = {-1,0,1,0};
int dy[] = {0,1,0,-1};
//        ио/ср/об/вС
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dir[6][3]={ {0,0,1},{0,0,-1},{-1,0,0},{1,0,0},{0,1,0},{0,-1,0} };
//int dir[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};

ll n,m;
ll a,b;
//对于一个数n,每次加一的代价是a,每次减一的代价是b,求被m整除时的最小代价
int main()
{
    while(cin>>n>>m>>a>>b)
    {
        ll ans;
        if(n%m==0)
        {
            cout<<"0"<<endl;
            continue;
        }
        else
        {
            if(n>m)
            {
                ans=min((n-m*(n/m))*b,(((n/m+1)*m-n)*a));
            }
            else ans=min(n*b,(m-n)*a);
        }
        cout<<ans<<endl;
    }
}
/*
9 7
9/7=1
9%7=2
↑

↓
7-2*2*2=1
4 8 1 5

7/2=3
7%2=1

8 3 3 7
8/3=2
8%3=2
2*7=14↓
1*3=3↑

9 6
6 6
12 6
9/6=1
9%6=3
*/
原文地址:https://www.cnblogs.com/Roni-i/p/9212917.html