错排+组合——HDU 2049

来源:互联网 发布:中国网络研究院 待遇 编辑:程序博客网 时间:2024/05/28 23:12
  • 题目链接:
    http://acm.hdu.edu.cn/showproblem.php?pid=2049

  • 分析:
    n对新婚夫妇,新郎找新娘,每次有m个新郎找错,求找错的方法有多少种

  • 题解:
    有m个新郎找错,即组合问题(nm);m个新郎找错的方法,即错排问题

  • AC代码:

/*************************************************************************    > File Name: test.cpp    > Author: Akira     > Mail: qaq.febr2.qaq@gmail.com  ************************************************************************/#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cmath>#include <vector>#include <set>#include <list>#include <ctime>typedef long long LL;typedef unsigned long long ULL;typedef long double LD;#define MST(a,b) memset(a,b,sizeof(a))#define CLR(a) MST(a,0)#define Sqr(a) ((a)*(a))using namespace std;#define MaxN 100000#define MaxM MaxN*10#define INF 0x3f3f3f3f#define bug cout<<88888888<<endl;int n,m;LL dp[30][30];LL a[25];void init(){    a[0] = a[1] = 0;    a[2] = 1;    a[3] = 2;    for(int i=4;i<25;i++)    {        a[i] = (i-1)*(a[i-1]+a[i-2]);    }    for(int i=0;i<=20;i++)    {        for(int j=0;j<=i;j++)         {            if(!j || i==j)                dp[i][j] = 1;            else                dp[i][j] = dp[i-1][j-1] + dp[i-1][j];                 //cout << i << "  " << j << ":  " << dp[i][j] << endl;         }    }    return;}int main(){    init();    int C;    scanf("%d", &C);    while(C--)    {        scanf("%d%d", &n, &m);        cout << a[m] << endl;        cout << dp[n][n-m]*a[m] << endl;    }    //system("pause");}
0 0
原创粉丝点击