bzoj4517: [Sdoi2016] permutation count-math + expand Euclid

Source: Internet
Author: User

This problem is a mathematical problem, by the title, m stable number of the way is CNM

Then the book is left N-m, because the book numbered I cannot be placed in the I position, so its method number should be determined by the wrong row formula, that is D (n-m)

Mismatch formula: d[i]= (i-1) * (D[i-1]+d[i-2]);

So according to the multiplication principle, the answer is CNM * D (N-M)

The next question is how to find the number of combinations.

Because of the n≤1000000, it is only possible to use the O (n) algorithm for the combination, where the multiplication inverse (inv[]) is used to aid the combinatorial number

That is Cnm = n! /((N-M)! * m!) = Fac[n]*inv[n-m]*inv[m]

So what is the multiplication inverse?

Assume a number a, and a multiplication inverse of P is x

So ax≡1 (mod P). When and only if A and P coprime, X has a solution

To put it simply, it is to find a number x that makes (x*a) mod P = 1

It is not difficult to conclude that the three conform to Ax+py=1 (Bezout theorem), Y may be negative

So we can use the expansion Euclidean to figure out the value of X, which is the multiplication inverse (saved with inv)

In order to find the process of inv, we can not need each violence to expand Euclid, can be obtained by the following recursive O (n)

inv[i]= (i+1) *inv[i+1]

and D array as long as O (n) push can, wherein d[0]=1, d[1]=0;

This question makes me understand. The number of combinations can be obtained by O (n), understand what the multiplication inverse is, and understand that there is a magic thing called the wrong row formula in the world Orz

#include <stdio.h>#include<algorithm>#include<string.h>#defineLL Long Longusing namespacestd;Const intMAXN =1000005;ConstLL MOD = 1e9+7;intt,n,m; LL F[maxn],inv[maxn],d[maxn];inlinevoidReadint&x) {    CharC=getchar (); x=0;  while(c<'0'|| C>'9') c=GetChar ();  while(c>='0'&& c<='9') x=x*Ten+c- -, c=GetChar ();} inline ll EX_GCD (ll&x, LL &y, ll A, LL b) {    if(b==0) {x=1; y=0; returnA; } LL Res=EX_GCD (x,y,b,a%b); LL T=x; x=y; Y=t-a/b*x; returnRes;}    Inline ll Calc (ll A, ll b) {ll x, y; if(EX_GCD (x,y,a,b) = =1LL)return(x+b)%b;}intMain () {read (T); f[0]=1;  for(intI=1; i<=maxn; i++) f[i]=f[i-1] * (LL) I%MOD; inv[1000000]=calc (f[1000000],mod);  for(inti=maxn-6; i>=0; i--) inv[i]=inv[i+1] * (LL) (i+1) %MOD; d[0]=1; d[1]=0; d[2]=1;  for(intI=3; i<=maxn; i++) d[i]= (LL) (I-1) * (d[i-1]+d[i-2]) %MOD;  while(t--) {read (n); read (m); LL ans=1LL; //printf ("haha%lld%lld%lld%lld\n", F[n], inv[n-m], inv[m], d[n-m]);ANS=ANS*F[N]*INV[N-M]%MOD; Ans=ANS*INV[M]%MOD; Ans=ANS*D[N-M]%MOD; printf ("%lld\n", ans); }    return 0;}

bzoj4517: [Sdoi2016] permutation count-math + expand Euclid

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.