1. 程式人生 > >2985:數字組合

2985:數字組合

style 01背包 tle clu ams 不同 ram using space

2985:數字組合

  • 查看
  • 提交
  • 統計
  • 提問
總時間限制:
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


關鍵字:01背包,逆序
 1 //*************************************
2 //7-1 PAx: 620終端 3 // Name: 2728:摘花生 4 // Solut: D.P. 5 //************************************ 6 #include "stdafx.h" 7 #include<bits/stdc++.h> 8 #define N 21 9 using namespace std; 10 int n, k; 11 int a[N],f[N*N*N]; 12 int main() 13 { 14 scanf("%d%d", &n,&k); 15 for (int i = 1
; i <= n; i++) 16 { 17 scanf("%d", &a[i]); 18 } 19 f[0] = 1; 20 for (int i = 1; i <= n; i++) 21 { 22 for (int j = k; j >= a[i]; j--) 23 { 24 f[j] += f[j - a[i]]; 25 } 26 } 27 printf("%d\n", f[k]); 28 return 0; 29 }

2985:數字組合