bzoj4517 "SDOI2016" permutation count

Source: Internet
Author: User

4517: [Sdoi2016] permutation count time limit: Sec Memory Limit: 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≤1000000output

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




modulus of combination number + 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 1000000007using 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 ();p rintf ("%lld\n", fac[n]* INV[M]%MOD*INV[N-M]%MOD*F[N-M]%MOD);} return 0;} 


bzoj4517 "SDOI2016" permutation count

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.