题目链接
题意:
现有n个本子,现需买k个本子使n+k被4整除。其中有三种方案:
(1)花费a购买一个本子
(2)花费b购买两个本子
(3)花费c购买三个本子
最少需要花费多少钱购买k个本子使n+k被4整除。
思路:
n对4取余,得到最少需要的个数。余数为0时,不需要再购买;余数为1时,最少需要购买3个,可行的方案:
(1)花费3*a ,得到3个
(2) 花费a+b,得到3个
(3)花费c,得到3个,同样被4整除
上面三种包含了所有花费最低的方案,比如可以花费b+c,3*a+c等,同样n+k被4整除,但花费要高。上面三种方案的最低花费即为余数为1对应的最低花费。对于余数为2、3时,同样有对应的情况。
code:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include<unordered_set>
#define debug(x) cout << #x << ":" << x << endl;
#define bug cout << "********" << endl;
#define IOS ios::sync_with_stdio(false), cin.tie(0);
#define fi first
#define se second
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const int mod = 1e9 + 7;
const int maxn = 2e6 + 10;
ll n, a, b, c;
int main()
{
cin >> n >> a >> b >> c;
ll s = n % 4, ans;
if(s==0)
ans = 0;
else if(s==1){
ans = min(3 * a, min(b + a, c));
}
else if(s==2){
ans = min(2 * a, min(b, 2 * c));
}
else {
ans = min(a, min(3 * c, b + c));
}
cout << ans << endl;
return 0;
}