標籤:
放蘋果
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 27339 |
|
Accepted: 17355 |
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
1 #include <iostream> 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <math.h> 5 #include <string.h> 6 using namespace std; 7 typedef long long ll; 8 int n,m,cou,a[100]; 9 void dfs(int k,int nn,int mm)///解法110 {11 //printf("%d--",cou);12 if(mm>m) return ;13 if(nn==0)14 {15 if(mm<=m)16 {17 cou++;18 }19 return;20 }21 for(int i=k; i<=nn; i++)///第i個數不小於第i-1個數,這樣不會造成重複。22 {///如果是從1一直到nn,那麼就會產生所有的排列情況,會有重複。23 dfs(i,nn-i,mm+1);24 }25 }26 int f(int nn,int mm)///解法227 {///整數劃分28 ///所謂n關於m的劃分,就是n分成任意個非負數部分,但是每一個部分都不超過m29 ///兩種情況: 1 至少存在一個m,相當與n-m後再進行m的劃分。30 /// 2 不存在m,也就是n關於轉化成m-1的劃分。31 if(nn<0) return 0;32 if(nn==1||mm==1) return 1;33 return f(nn-mm,mm)+f(nn,mm-1);34 }35 int main()36 {37 int t;38 scanf("%d",&t);39 while(t--)40 {41 scanf("%d%d",&n,&m);42 /*{cou=0;43 dfs(1,n,0);44 printf("%d\n",cou);}*/45 printf("%d\n",f(n,m));46 }47 return 0;48 }View Code
暴力放蘋果