CF544 C 背包 DP
n个人写m行代码,第i人写一行代码有a[i]个bug,问总bug数不超过b的不同方案数。
其实就是个背包,dp[i][j][k]代表前i个人写了j行代码用了k个bug限度,然后随便转移一下就好了
/** @Date : 2017-08-27 21:13:25 * @FileName: C 完全背包DP.cpp * @Platform: Windows * @Author : Lweleth (SoungEarlf@gmail.com) * @Link : https://github.com/ * @Version : $Id$ */ #include <bits/stdc++.h> #define LL long long #define PII pair<int ,int> #define MP(x, y) make_pair((x),(y)) #define fi first #define se second #define PB(x) push_back((x)) #define MMG(x) memset((x), -1,sizeof(x)) #define MMF(x) memset((x),0,sizeof(x)) #define MMI(x) memset((x), INF, sizeof(x)) using namespace std; const int INF = 0x3f3f3f3f; const int N = 1e5+20; const double eps = 1e-8; LL dp[600][600]; LL v[600]; LL mod, n, m, b; int main() { while(cin >> n >> m >> b >> mod) { for(int i = 0; i < n; i++) scanf("%lld", v + i); MMF(dp); dp[0][0] = 1; for(int i = 0; i < n; i++) { for(int j = 1; j <= m; j++) { for(int k = 0; k <= b; k++) { if(k < v[i]) continue; dp[j][k] = (dp[j][k] + dp[j - 1][k - v[i]] + mod) % mod; } } } LL ans = 0; for(int i = 0; i <= b; i++) ans = (ans + dp[m][i] + mod) % mod; printf("%lld\n", ans); } return 0; }