- 首页 > it技术 > >
dp|codeforces543A Writing Code(完全背包)
//完全背包
//首先定义dp[i][j][k]为前i个人写j行所含bug为k个的种数
//然后对于dp[i][j][k]=dp[i-1][j][k]+dp[i][j-1][k-bug[i]];
//第i个人要么不写,要么再写一行
//然后用类似完全背包我们可以通过顺序遍历,省掉一维i
import java.io.*;
import java.util.*;
public class cf {
FastScanner in;
long max(long a,long b){
if(a>b)return a;
return b;
}
void input(){
in = new FastScanner(System.in);
int n=in.nextInt();
int m=in.nextInt();
int b=in.nextInt();
int mod=in.nextInt();
int v[]=new int[3000];
int dp[][]=new int[600][600];
int cnt=0;
for(int i=1;
i<=n;
i++){
v[i]=in.nextInt();
}
dp[0][0]=1;
for(int i=1;
i<=n;
i++){
for(int j=1;
j<=m;
j++){
for(int k=v[i];
k<=b;
k++){
dp[j][k]=(dp[j][k]+dp[j-1][k-v[i]])%mod;
}
}
}
int ans=0;
for(int i=0;
i<=b;
i++){
ans=(ans+dp[m][i])%mod;
}
System.out.println(ans);
}
public static void main(String[] args){
new cf().input();
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}int nextInt() {
return Integer.parseInt(next());
}long nextLong() {
return Long.parseLong(next());
}double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
推荐阅读