POJ1664——放蘋果

來源:互聯網
上載者:User

標籤: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——放蘋果

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.