标签:style http color os io 数据 for 代码

2 2 2 3 2
1 3
假设一共有N对新婚夫妇,其中有M个新郎找错了新娘,求发生这种情况一共有多少种可能.
首先从N个新郎里面选出M个,然后乘以M的错位全排列就可以了。
注意错排20的范围就超过int类型了...
代码:
#include <iostream>
using namespace std;
const int maxn=21;
typedef long long ll;
ll f[maxn];
ll c[maxn][maxn];
void init()
{
f[0]=1,f[1]=0;
for(int i=2;i<maxn;i++)
f[i]=(i-1)*(f[i-1]+f[i-2]);
c[0][0]=1;c[1][0]=1;c[1][1]=1;
for(int i=2;i<maxn;i++)
{
c[i][0]=c[i][i]=1;
for(int j=1;j<i;j++)
c[i][j]=c[i-1][j]+c[i-1][j-1];
}
}
int main()
{
init();
int t,n,m;
cin>>t;
while(t--)
{
cin>>n>>m;
cout<<c[n][m]*f[m]<<endl;
}
return 0;
}
[ACM] hdu 2049 不容易系列之(4)——考新郎 (组合+错排),布布扣,bubuko.com
[ACM] hdu 2049 不容易系列之(4)——考新郎 (组合+错排)
标签:style http color os io 数据 for 代码
原文地址:http://blog.csdn.net/sr_19930829/article/details/38293817