zoukankan      html  css  js  c++  java
  • bzoj-4517 4517: [Sdoi2016]排列计数(组合数学)

    题目链接:

    4517: [Sdoi2016]排列计数

    Time Limit: 60 Sec  Memory Limit: 128 MB
    Submit: 846  Solved: 530
    [Submit][Status][Discuss]

    Description

    求有多少种长度为 n 的序列 A,满足以下条件:
    1 ~ n 这 n 个数在序列中各出现了一次
    若第 i 个数 A[i] 的值为 i,则称 i 是稳定的。序列恰好有 m 个数是稳定的
    满足条件的序列可能很多,序列数对 10^9+7 取模。

    Input

    第一行一个数 T,表示有 T 组数据。
    接下来 T 行,每行两个整数 n、m。
    T=500000,n≤1000000,m≤1000000
     

    Output

    输出 T 行,每行一个数,表示求出的序列数

     

    Sample Input

    5
    1 0
    1 1
    5 2
    100 50
    10000 5000

    Sample Output

    0
    1
    20
    578028887
    60695423
     
    题意:
     
    思路:
     
    我们很容易知道方案数是C(n,m)*dp[n-m];
    dp[n]表示n的错排数;递推公式是dp[n]=(n-1)*(dp[n-1]+dp[n-2])=n*dp[n-1]+(-1)n ;
     
    AC代码:
    /**************************************************************
        Problem: 4517
        User: LittlePointer
        Language: C++
        Result: Accepted
        Time:11108 ms
        Memory:16916 kb
    ****************************************************************/
     
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const LL mod=1e9+7;
    const int maxn=1e6+10;
    LL dp[maxn],p[maxn];
    inline void init()
    {
        dp[0]=1;dp[1]=0;dp[2]=1;p[1]=1;p[2]=2;p[0]=1;
        for(int i=3;i<maxn;i++)dp[i]=(LL)(i-1)*(dp[i-1]+dp[i-2])%mod,p[i]=p[i-1]*(LL)i%mod;
    }
    LL pow_mod(LL x,LL y)
    {
        LL s=1,base=x;
        while(y)
        {
            if(y&1)s=s*base%mod;
            base=base*base%mod;
            y>>=1;
        }
        return s;
    }
    int main()
    { 
        //freopen("in.txt","r",stdin);
        init();
        int t,n,m;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d",&n,&m);
            if(m>n){puts("0");continue;}
            LL ans=p[n]*dp[n-m]%mod,temp=p[m]*p[n-m]%mod;
            ans=ans*pow_mod(temp,mod-2)%mod;
            printf("%lld
    ",ans);
        }
        return 0;
    }
    

      

     
  • 相关阅读:
    Mac查看某个文件的中某关键字信息
    Mac查看所有的文件
    Mac查看当前用户的环境变量
    Mac安装maven
    Lombok使用坑之属性不区分大小写
    Mac常用软件列表
    Mac系统升级
    Mac安装Git
    Mac查看git的安装路径
    Mac安装JDK8
  • 原文地址:https://www.cnblogs.com/zhangchengc919/p/5983049.html
Copyright © 2011-2022 走看看