4517: [Sdoi2016] permutation count Time limit:60 Sec Memory limit:128 MB
submit:576 solved:358
[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
Output T-line, one number per line, for the number of sequential numbers to find Sample Input 5
1 0
1 1
5 2
100 50
10000- Sample Output 0
1
20
578028887
60695423 HINT
Source
Acknowledgement Menci Upload
Combination number modulo + wrong row problem
First, it is obvious that the answer equals C (n,m) *f[n-m], where f[n-m] represents the number of scenarios where the number of (N-M) is all misplaced.
As long as the inverse of factorial and factorial can be preprocessed, the problem is how to find the F-array quickly.
For the F array, there is a recursive relationship f[i]= (f[i-1]+f[i-2]) * (I-1), the formula for this staggered scheme is still to be remembered ...
The proof is as follows:
1-n Total n number of all row wrong, assuming that 1 row to the position K (k≠1).
If K is ranked at position 1, then the number of remaining (n-2) is all wrong and the scheme count is f[n-2]. If K is not ranked to position 1, consider the position k to position 1 has a "portal", that is equal to the number of 2-k all the wrong, the scheme number is f[n-1]. K has a value of (n-1), then f[n]= (F[n-1]+f[n-2]) * (n-1).
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstring>
#include <algorithm>
#define F (i,j,n) for (int i=j;i<=n;i++)
#define D ( i,j,n) for (int i=j;i>=n;i--)
#define LL Long long
#define MAXN 1000005
#define MOD 1000000007
using namespace Std;
int t,n,m,x,y;
ll F[MAXN],FAC[MAXN],INV[MAXN];
inline int read ()
{
int x=0,f=1;char ch=getchar ();
while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();}
while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();}
return x*f;
}
int main ()
{
fac[0]=1;
F (i,1,1000000) fac[i]=fac[i-1]*i%mod;
Inv[0]=1;inv[1]=1;
F (i,2,1000000)
{
x=mod/i+1;y=x*i-mod;
Inv[i]=inv[y]*x%mod;
}
F (i,1,1000000) inv[i]=inv[i-1]*inv[i]%mod;
f[0]=1;f[1]=0;
F (i,2,1000000) f[i]= (F[i-1]+f[i-2])%mod* (i-1)%mod;
T=read ();
while (t--)
{
n=read (); M=read ();
printf ("%lld\n", Fac[n]*inv[m]%mod*inv[n-m]%mod*f[n-m]%mod);
}
return 0;
}