首页 > 代码库 > Codeforces 740A Alyona and copybooks

Codeforces 740A Alyona and copybooks

题意:某人现有n本书,想再拥有k本书,使得(n % k) == 0,求买k本书最少花多少钱。已知a元买1本书,b元买2本书,c元买3本书。

分析:n对4取余,可分为4种情况:(t = n % 4)(每个情况下考虑a,b,c的所有组合)

1、t == 0,则不必花钱买书。

2、t == 3,还需买1本,

(1)用a元买1本

(2)用c元买3本(因为a可能很大,然后c很小)

(3)肯定不能完全用b元买,因为整除不了,此外肯定不能用a + b 或 a + c买,因为这样花的钱大于a,所以就用b + c买1本

3、t == 2,还需买2本,同理,完全用a买,完全用b买,完全用c买,没必要用a + b,a + c等买

4、t == 1,还需买3本,与上面同理

#pragma comment(linker, "/STACK:102400000, 102400000")
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define Min(a, b) ((a < b) ? a : b)
#define Max(a, b) ((a < b) ? b : a)
typedef long long ll;
typedef unsigned long long llu;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const ll LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const double eps = 1e-8;
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
int main(){
    ll n, a, b, c;
    while(scanf("%lld%lld%lld%lld", &n, &a, &b, &c) == 4){
        if(n % 4 == 0){
            printf("0\n");
            continue;
        }
        ll tmp = ll(4) - ll(n % 4);
        if(tmp == ll(1)){
            ll t = Min(a, 3 * c);
            t = Min(t, b + c);
            printf("%lld\n", t);
            continue;
        }
        if(tmp == ll(2)){
            ll t = Min(2 * a, b);
            t = Min(2 * c, t);
            printf("%lld\n", t);
            continue;
        }
        if(tmp == (3)){
            ll t = Min(3 * a, c);
            t = Min(t, a + b);
            printf("%lld\n", t);
            continue;
        }
    }
    return 0;
}

 

Codeforces 740A Alyona and copybooks