4517: [Sdoi2016] permutation count time limit:60 Sec Memory limit:128 MB
submit:637 solved:396
[Submit] [Status] [Discuss] Description How many sequence a of length n, satisfies the following conditions: 1 ~ n the number of n is present in the sequence once, if the number of I a[i] is the value of I, then I is said to be stable. Sequences that have exactly m number are stable enough to satisfy the condition of the sequence may be many, sequence number pairs 10^9+7 modulo. The first line of input is a number T, which indicates that there is a T group of data. Next T line, two integers per line n, M. t=500000,n≤1000000,m≤1000000 Output
Outputs T line, one number per line, indicating the number of sequences to be calculated
Sample Input5
1 0
1 1
5 2
100 50
10000Sample Output0
1
20
578028887
60695423HINT Source
Acknowledgement Menci Upload
The answer is C (n,m) *f[n-m],f[n-m], which indicates the number of schemes (N-M) that have the wrong number of rows.
Wrong row, poking around.
Fn= (n-1) (fn-1+fn-2)
preprocessing can be done.
1#include <bits/stdc++.h>2 #defineLL Long Long3 Const intOO =0x3f3f3f3f;4Template <classT> InlineBOOLChkmin (t &x, t y) {returnx > y? x = y,true:false; }5Template <classT> InlineBOOLChkmax (t &x, t y) {returnx < y? x = y,true:false; }6Template <classT> inline T read (T &x)7 {8 Static intF;9 Static CharC;Ten for(f =1;!isdigit (c = GetChar ()); )if(c = ='-') F =-F; One for(x =0; IsDigit (c); c = GetChar ()) x = x *Ten+ C- -; A returnX *=F; - } -Template <classT> InlinevoidWrite (T x,CharP =0) the { - Static Chars[ -]; - Static inttop =0; - if(X <0) x =-X, Putchar ('-'); + Do{S[++top] = x%Ten+ -;} while(x/=Ten); - Do{Putchar (s[top]);} while(--top); + if(P) Putchar (p); A } at - Static Const intMAXN =1e6; - Static Const intMO = 1e9 +7; - - Static intT, N, M; - Static intJC[MAXN +5], NI[MAXN +5], F[MAXN +5]; in -InlineintQpow (intXintN) to { + intAns =1; - while(n) { the if(N &1) ans = (LL) ans * x%MO; *x = (LL) x * x%MO; $N >>=1;Panax Notoginseng } - returnans; the } + A intMain () the { +ni[0] = jc[0] =1; - for(Registerinti =1; I <= MAXN; ++i) { $Jc[i] = (LL) jc[i-1] * I%MO; $Ni[i] = Qpow (Jc[i], MO-2); - } -f[0] =1; f[1] =0; the for(Registerinti =2; I <= MAXN; ++i) { -F[i] = (LL) (F[i-1] + f[i-2]) * (I-1) %MO;Wuyi } the for(Read (T); t--; ) { - Read (N), read (M); WuWrite (LL) jc[n] * NI[N-M]% mo * NI[M]% mo * F[N-M]% mo,'\ n'); - } About return 0; $}
View Code
Bzoj 4517: [Sdoi2016] permutation count