http://codeforces.ru/contest/543/problem/A
Problem
N个人写代码,a[i]表示第i个人,写一行代码会出现a[i]个bug(不多也不少)。现在问,N个人总共写M行,出现bug总数不超过b的方案数(允许有人一行也不写)
数据范围
N,M,b,a[i] : [1,500]
Solution
先考虑最直接的dp。dp[i][j][k]表示前i个人,写了j 行,出现k个bug的方案数,那么转移,dp[i][j+j1][k+j1*a[i]]+=dp[i][j][k],枚举i,枚举j,枚举k,再枚举j1。这样就是O(N^4)的dp。
复杂度太高了,加小优化也是不过去的。想想看,枚举j1那一步可以省略。具体看代码
//Hello. I‘m Peter.
//#pragma comment(linker, "/STACK:102400000,102400000")
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define MAXN
#define N 505
#define M
int dp[N][N],n,m,mod,b,a[N];
int main(){
cin>>n>>m>>b>>mod;
for(int i=0;i<n;i++){
scanf("%d",a+i);
}
dp[0][0]=1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
for(int k=0;k<=b;k++){
if(k+a[i]>b||!dp[j][k]) continue;
dp[j+1][k+a[i]]+=dp[j][k];
if(dp[j+1][k+a[i]]>=mod) dp[j+1][k+a[i]]-=mod;
}
}
}
int ans=0;
for(int k=0;k<=b;k++){
ans+=dp[m][k];
if(ans>=mod) ans-=mod;
}
printf("%d\n",ans);
return 0;
}
时间: 2024-12-06 12:24:04