bzoj 4517 [Sdoi2016]排列計數

來源:互聯網
上載者:User

4517: [Sdoi2016]排列計數
Time Limit: 60 Sec Memory Limit: 128 MB
Submit: 1208 Solved: 734
[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

HINT

Source

鳴謝Menci上傳

【分析】
組合數+錯排數
山東oi竟然能出這種題OTZ

【代碼】

//bzoj 4517 [Sdoi2016]排列計數#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#define N 1000000#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=1000005;const int mod=1e9+7;int T,n,m;ll fac[mxn],inv[mxn],f[mxn];inline void init(){    int i,j;    fac[0]=inv[0]=inv[1]=f[0]=f[2]=1;    fo(i,1,N) fac[i]=fac[i-1]*i%mod;    fo(i,2,N) inv[i]=(ll)(mod-mod/i)*inv[mod%i]%mod;    fo(i,1,N) inv[i]=inv[i]*inv[i-1]%mod;    fo(i,3,N) f[i]=(ll)(i-1)*(f[i-1]+f[i-2])%mod;}inline ll C(int n,int m){    if(n<m) return 0;    return fac[n]*inv[m]%mod*inv[n-m]%mod;}int main(){    init();    int i,j;    scanf("%d",&T);    while(T--)    {        scanf("%d%d",&n,&m);        printf("%lld\n",C(n,m)*f[n-m]%mod);    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.