Writing Code-Codeforces511C**
http://codeforces.com/problemset/problem/544/C
完全背包
dp[i][j]表示第i行有j个bug
#include<stdio.h> #include<string.h> #define N 550 int dp[N][N], a[N]; int main() { int n, m, b, MOD; while(scanf("%d%d%d%d", &n, &m, &b, &MOD)!=EOF) { int i, j, k, w, sum=0; memset(dp, 0, sizeof(dp)); memset(a, 0, sizeof(a)); for(i=0; i<n; i++) scanf("%d", &a[i]); for(i=0; i<=m; i++) { w = i*a[i]; if(w<=b) dp[i][w] = 1; } for(i=1; i<n; i++) for(j=0; j<m; j++) for(k=0; k<=b; k++) { if(dp[j][k]) { int x = j-1, y = k + a[i]; if(y<=b) { dp[x][y] += dp[j][k]; dp[x][y] %= MOD; } } } for(i=0; i<=b; i++) { sum += dp[m][i]; sum %= MOD; } printf("%d\n", sum); } return 0; }