有M个小孩到公园玩,门票是1元。其中N个小孩带的钱为1元,K个小孩带的钱为2元。售票员没有零钱,问这些小孩共有多少种排队方法,使得售票员总能找得开零钱。注意:两个拿一元零钱的小孩,他们的位置互换,也算是一种新的排法。(M<=10)
有M个小孩到公园玩,门票是1元。其中N个小孩带的钱为1元,K个小孩带的钱为2元。售票员没有零钱,问这些小孩共有多少种排队方法,使得售票员总能找得开零钱。注意:两个拿一元零钱的小孩,他们的位置互换,也算是一种新的排法。(M<=10)
输入一行,M,N,K(其中M=N+K,M<=10)。
输出一行,总的排队方案。
4 2 2
#include<iostream> #include<algorithm> #include<string> #include<map><span style="color:#0000cd;"><strong> </strong></span>#include<vector> #include<cmath> #include<string.h> #include<stdlib.h> #include<cstdio> #define ll long long using namespace std; int main(){ int m,n,k; cin>>m>>n>>k; if(n<k) cout<<0<<endl; //2元小朋友比1元小朋友多 else{ int s1=1; for(int i=1;i<=k;++i) //2元的小朋友内部全排列 s1*=i; int s2=1; for(int i=1;i<=n;++i) //1元的小朋友内部全排列 s2*=i; int dp[20][20]={0}; dp[1][0]=1; dp[1][1]=1; for(int i=2;i<=n;++i){ for(int j=0;j<=i;++j){ for(int g=0;g<=j;++g){ dp[i][j]+=dp[i-1][g];}}} //由i个1元小朋友和j个2元小朋友组成的队伍排法 cout<<dp[n][k]*s2*s1<<endl; } return 0; }
#include<iostream> #include<algorithm> #include<map>//ll dx[4]={0,0,-1,1};ll dy[4]={-1,1,0,0}; #include<set>// #include<vector> #include<cmath> #include<stack> #include<string.h> #include<stdlib.h> #include<cstdio> #include<string> #define mod 1000000007 #define eps 1e-6 #define lowbit(x) (x) & (-x) using namespace std; int main(){ int m,n,k; cin>>m>>n>>k; int dp[20][20]={0}; dp[0][0]=1; for(int i=1;i<=m;++i){ for(int j=0;j<=i;++j){ if(j>0) dp[i][j]+=dp[i-1][j-1]; dp[i][j]+=dp[i-1][j+1]; } } if(n<k){ cout<<0<<endl; //2元小朋友比1元小朋友多 return 0; } int s1=1; for(int i=1;i<=k;++i) //2元的小朋友内部全排列 s1*=i; int s2=1; for(int i=1;i<=n;++i) //1元的小朋友内部全排列 s2*=i; cout<<dp[m][abs(n-k)]*s2*s1<<endl; }二维略难懂。
令h(1)=1,h(0)=1,数满足递归式: h(n)= h(0)*h(n-1)+h(1)*h(n-2) + ... +h(n-1)h(0) (其中n>=2)
或:h(n)=h(n-1)*(4*n-2)/(n+1); 该递推关系的解为:
h(n)=C(2n,n)/(n+1) (n=1,2,3,...)