容斥。ios
總方案數 - 不合法方案數。spa
#include <cmath> #include <queue> #include <cstdio> #include <cctype> #include <cstring> #include <iostream> #include <algorithm> using namespace std; const int maxn = 100000 + 10; int n, val[5], num[5]; long long dp[maxn], sum, ans; inline long long Div(int x) { return 1ll * val[x] * (num[x] + 1); } int main(int argc, char const *argv[]) { scanf("%d%d%d%d", val + 1, val + 2, val + 3, val + 4), scanf("%d", &n), dp[0] = 1; for(int k = 1; k <= 4; ++k) for(int i = 1; i < maxn; ++i) if( i - val[k] + 1 > 0 ) dp[i] = dp[i] + dp[i - val[k]]; while( ~scanf("%d%d%d%d", num + 1, num + 2, num + 3, num + 4) ) { scanf("%d", &n), ans = 0; for(int f = 1, i = 0; i < 16; ++i) { sum = 0, f = 1; for(int j = 0; j < 4; ++j) if( (i >> j) & 1 ) sum = sum + Div(j + 1), f = -f; if( sum <= n ) ans = ans + f * dp[n - sum]; } printf("%lld\n", ans); } return 0; }