標籤:dp
放蘋果
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 26595 |
|
Accepted: 16906 |
Description
把M個同樣的蘋果放在N個同樣的盤子裡,允許有的盤子空著不放,問共有多少種不同的分法?(用K表示)5,1,1和1,5,1 是同一種分法。
Input
第一行是測試資料的數目t(0 <= t <= 20)。以下每行均包含二個整數M和N,以空格分開。1<=M,N<=10。
Output
對輸入的每組資料M和N,用一行輸出相應的K。
Sample Input
17 3
Sample Output
8
Source
[email protected]
簡單的dp題,設dp[i][j]表示把i個蘋果放入j個盤子裡的方案數,盤子允許有空
如果i 或者j為1,那麼方案就是1,如果i < j,必然有空盤子,但是空哪幾個是無所謂的,所以 此時dp[i][j] = dp[i][i];
如果i == j,那麼方案就是把i個蘋果放入j-1個盤子的方案再加上一種平攤的方案
如果i > j,要麼至少有一個盤子空,要麼都有(先拿出j個平攤到j個盤子,在考慮剩下的i-j個蘋果)
#include <map> #include <set> #include <list> #include <stack> #include <queue> #include <vector> #include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std;int dp[15][15];int main(){int t, m, n;memset ( dp, 0, sizeof(dp) );for (int i = 1; i <= 11; ++i){for (int j = 1; j <= 11; ++j){if (i == 1 || j == 1){dp[i][j] = 1;}if (i < j){dp[i][j] = dp[i][i];}else if (i == j){dp[i][j] = dp[i][j - 1] + 1;}else{dp[i][j] = dp[i][j - 1] + dp[i - j][j];}}}scanf("%d", &t);while (t--){scanf("%d%d", &m, &n);printf("%d\n", dp[m][n]);}return 0;}
POJ1664——放蘋果