[HAOI2008]硬币购物 (容斥原理)

传送门

code

/*************************************************************************
> File Name: 1.cpp
> Author: Knowledge_llz
> Mail: 925538513@qq.com
> Blog: https://www.cnblogs.com/Knowledge-Pig/ 
************************************************************************/

#include<bits/stdc++.h>
#define LL long long
#define endl '\n'
using namespace std;
const int maxx = 1e5 + 10;
LL q, s, c[5], d[5], dp[maxx];
int main(){
    ios::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
    freopen("input.in", "r", stdin);
    freopen("output.out","w", stdout);
#endif
    for(int i = 1; i <= 4; ++i) cin >> c[i];
    cin >> q;
    dp[0] = 1;
    for(int i = 1; i <= 4; ++i){
        for(int j = c[i]; j <= 100000; ++j)
            dp[j] += dp[j - c[i]];
    }
    while(q--){
        for(int i = 1; i <= 4; ++i) cin >> d[i];
        cin >> s;
        LL ans = dp[s], tmp, num;
        for(int i = 1; i < 16; ++i){
            tmp = s; num = 0;
            for(int j = 0; j < 4; ++j)
                if(i & (1 << j))
                    tmp -= (d[j + 1] + 1) * c[j + 1], ++num;
            if(tmp >= 0) ans += ((num & 1) ? -dp[tmp] : dp[tmp]);
        }
        cout << ans << endl;
    }
    return 0;
}
posted @ 2021-11-25 22:38  Knowledge-Pig  阅读(12)  评论(0编辑  收藏  举报