天天看點

OpenJudge_P2985 數的組合(DP)

總時間限制: 1000ms 記憶體限制: 65536kB

描述

有n個正整數,找出其中和為t(t也是正整數)的可能的組合方式。如:

n=5,5個數分别為1,2,3,4,5,t=5;

那麼可能的組合有5=1+4和5=2+3和5=5三種組合方式。

輸入

輸入的第一行是兩個正整數n和t,用空格隔開,其中1<=n<=20,表示正整數的個數,t為要求的和(1<=t<=1000)

接下來的一行是n個正整數,用空格隔開。

輸出

和為t的不同的組合方式的數目。

樣例輸入

5 5

1 2 3 4 5

樣例輸出

3

#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std;
#define N 50 
int n,t,ans,x;
int f[N];
int main(){
    scanf("%d%d",&n,&t);f[]=;
    for(int i=;i<=n;i++){
        scanf("%d",&x);
        for(int j=t;j>=x;j--){
            f[j]+=f[j-x];
        }
    }
    printf("%d",f[n]);
}