题目:
http://codeforces.com/problemset/problem/740/A
题意:
给定n, a, b, c
分别表示:
目前已经买了n本书
书的价格:a元可买1本书,b元可买2本书,c元可买3本书
求ans(ans的含义为:用ans元,再买k本书,使得 (n + k) % 4==0 的最小 ans)
分析:
如果n已经是4的倍数,那么不必再花钱买书,已经符合题意了
否则,计算 r = 4 - n % 4; r + 4m(m = 1, 2, 3...)需要再买的本数的可能值
当 r=1时
买1本,需要a元;
买5(1+4*1)本,需要5a(一定大于a,舍去)或者 b+c元(b元买2本,c元买3本)
买9(1+4*2)本,需要9a(舍去)或者3c元(每次用c元买3本)
ans 取 a、b+c、3c中的最小值
同理,r=2时
买2本,需要 2a 元或者 b元
买6本,需要6a(舍去)、3b(舍去),或者2c元
故而ans 取 2a、b、2c中的最小值
同理,r=3时
ans取 3a、c、a+b中的最小值
代码:
#include <iostream>
using namespace std;
typedef long long LL;
LL n, a, b, c, ans;
void solve()
{
cin >> n >> a >> b >> c;
ans = 0;
if (n % 4 == 0)
{
cout << 0 << endl;
return;
}
int r = 4 - n % 4; //还需要买多少本
if (r == 1) // 1、5(2+3)、9(3*3)
{
ans = min(min(a, b + c), 3 * c);
}
else if (r == 2) // 2、6(2+2+2, 3+3)
{
ans = min(min(a * 2, b), c * 2);
}
else if (r == 3) // 1 * 3, 1 + 2, 3 * 1
{
ans = min(min(a * 3, c), a + b);
}
cout << ans << endl;
}
int main()
{
solve();
return 0;
}