zoukankan      html  css  js  c++  java
  • HDU 6143-排列数

    题意

    给出n和m,n代表first name和last name的长度,m代表字符种数,求有多少种排列使得first name和last name没有相同的字符。m个字符可以不用完。

    分析

    枚举整个名字用了$k in [2,m]$种字符,first name用了$i in [1,k-1]$种字符,则last name用了$k-i$种字符。

    first name排列数即用$n$个字符分成$i$组的排列数$S(n,i)*i!$

    last name排列数即$S(n,k-i)*(k-i)!$

    答案可表示为$sum_{k=2}^m sum_{i=1}^{k-1} (C(k,i)*S(n,i)*i!*S(n,k-i)*(k-i)!)$

    代码

    #include <map>
    #include <set>
    #include <queue>
    #include <cmath>
    #include <ctime>
    #include <vector>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <iostream>
    #include <algorithm>
    #define MAX     2007
    #define MAXN      10007
    #define MAXM      20007
    #define INF  0x3f3f3f3f
    #define NINF 0xc0c0c0c0
    #define MOD  1000000007
    using namespace std;
    typedef long long LL;
    LL C[MAX][MAX],S[MAX][MAX],AA[MAX]={1,1};
    void init(){
        S[1][1]=1;
        for(int i=2;i<MAX;i++)AA[i]=AA[i-1]*i%MOD;
        for(int i=0;i<MAX-1;i++){
            C[i][0]=C[i][i]=S[i][i]=S[i][1]=1;
            for(int j=1;j<i;j++){
                C[i][j]=(C[i-1][j-1]+C[i-1][j])%MOD;
                S[i][j]=(S[i-1][j-1]+j*S[i-1][j]%MOD)%MOD;
            }
        }
    }
    LL n,m;
    LL ac(LL n,LL m){
        LL ans=0;
        for(int i=1;i<m;i++){
            LL d=C[m][i]*S[n][i]%MOD;
            d=d*AA[i]%MOD;
            d=d*S[n][m-i]%MOD;
            d=d*AA[m-i]%MOD;
            ans=(ans+d)%MOD;
        }
        return ans;
    }
    int main(){
        init();
        int cas;
        scanf("%d",&cas);
        while(cas--){
            scanf("%lld%lld",&n,&m);
            LL ans=0;
            for(int i=2;i<=m;i++){
                LL d=ac(n,i)*C[m][i]%MOD;
                ans=(ans+d)%MOD;
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    

      

  • 相关阅读:
    小程序开发 access_token 统一管理
    python操作mysql
    Mac版本的idea非正常关闭后,idea打开项目大面积报红
    PySpider爬取去哪儿攻略数据项目
    Python3.9安装PySpider步骤及问题解决
    Selenium 自动化测试工具
    Python 抓取猫眼电影排行
    Python爬虫基本库
    Python 创建一个Django项目
    Python 数据可视化
  • 原文地址:https://www.cnblogs.com/shuiming/p/7383894.html
Copyright © 2011-2022 走看看