zoukankan      html  css  js  c++  java
  • BZOJ 4517: [Sdoi2016]排列计数

    4517: [Sdoi2016]排列计数

    Time Limit: 60 Sec  Memory Limit: 128 MB
    Submit: 911  Solved: 566
    [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
     

    错排递推式:f(n)=(n-1)*[f(n-1)+f(n-2)] f[0]=1,f[1]=0,f[2]=1;
    显然本题中确定的位置有:${{C}_{n}^{m}}$种可能的组合
    剩下来的位置全部都要错排,即套用错排公式即可$${ans={C}_{n}^{m}*F[n-m]}$$

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<vector>
     5 #include<cstdlib>
     6 #include<cmath>
     7 #include<cstring>
     8 using namespace std;
     9 #define maxn 1000010
    10 #define llg long long 
    11 #define yyj(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
    12 #define md 1000000007
    13 
    14 llg T,n,m;
    15 llg D[maxn],N[maxn];
    16 
    17 void make_D()//错排递推式 f(n)=(n-1)*[f(n-1)+f(n-2)]
    18 {
    19     D[1]=0,D[2]=1;
    20     for (llg i=3;i<=maxn-5;i++) D[i]=(i-1)*(D[i-1]+D[i-2]),D[i]%=md;
    21 }
    22 
    23 void maken()
    24 {
    25     N[1]=1;
    26     for (llg i=2;i<=maxn-5;i++) N[i]=N[i-1]*i,N[i]%=md;
    27 }
    28 
    29 llg ksm(llg a,llg b,llg c)
    30 {
    31     if (b==0) return 1;
    32     a%=md;
    33     llg ans=1;
    34     while (b!=0)
    35     {
    36         if (b%2) ans*=a,ans%=md;
    37         b/=2;
    38         a*=a, a%=md;
    39     }
    40     return ans;
    41 }
    42 
    43 int main()
    44 {
    45     cin>>T;
    46     N[0]=D[0]=1;
    47     maken(),make_D();
    48     while (T--)
    49     {
    50         scanf("%lld%lld",&n,&m);
    51         llg x=(N[n-m]*N[m]) % md;
    52         llg ni=ksm(x,md-2,md);
    53         printf("%lld
    ",((N[n]*ni) % md)*D[n-m] % md);
    54     }
    55     return 0;
    56 }
    本文作者:xrdog 作者博客:http://www.cnblogs.com/Dragon-Light/ 转载请注明出处,侵权必究,保留最终解释权!
  • 相关阅读:
    .net core ELK
    mongodb基础
    .net core FluentValidation
    使用Identity Server 4建立Authorization Server
    .net core JWT应用
    .net core action过滤器的普通应用
    matplotlib
    python-13:文件操作 之二
    python-13:文件操作 open
    python-12:内置 函数之一 filter map sorted reduce
  • 原文地址:https://www.cnblogs.com/Dragon-Light/p/6219718.html
Copyright © 2011-2022 走看看