NTT "51nod" 1514 wonderful sequence

Source: Internet
Author: User

Test instructions: In the full arrangement of the 1~n, how many permutations meet arbitrarily from the middle into two segments, the left segment maximum is greater than the right segment of the minimum value?

For example, there are 3 types when n is 3

2 3 1

3 1 2

3 2 1

Explanation: For example 2 3 1

(2) (3 1) 1:2 Small

(2 3) (1) 1:2 Small

All meet the above conditions.

3 2 1

(3) (2 1) 1:3 Small

(32) (1) 1:3 Small

All meet the above conditions.

and 2 1 3 is not satisfied, because (2 1) (3), 3 is larger than all the numbers on the left.

==================================== Split Line ====================================

First sequence for wonderful equivalence to non-existence (1<=i<n) makes the first I number of 1~i permutations

Make F[n] The answer to the length n

The

F[0]=0

F[i]=i!-sigma (f[j]* (I-J)!) 0<=j<i

We will deform it

Sigma (f[j]* (i-j)!) = i! I > 0, j=0~i

Sigma (f[j]* (i-j)!) = 0 i = 0, j=0~i

Make g (x) =sigma (i!*x^i), F (x) =sigma (f[i]*x^i)

F (x) *g (x) =g (x)-1 (minus one for i = 0)

F (x) = (G (x)-1)/g (x) =1-1/g (x)

Polynomial inversion can be

1#include <cstdio>2#include <iostream>3typedefLong Longll;4 using namespacestd;5 Const intN =262144, K = -;6 intN, M, I, K;7 inta[n+Ten], b[n+Ten], tmp[n+Ten], tmp2[n+Ten];8 intP =998244353, G =3, g[k+1], ng[k+Ten], inv[n+Ten], inv2;9 intPowintAintb) {intt=1; for(; b;b>>=1, A= (LL) a*a%p)if(b&1) t= (ll) t*a%p;returnt;}Ten voidNTT (int*a,intNintt) { One      for(intI=1, j=0; i<n-1; i++){ A          for(ints=n;j^=s>>=1,~j&s;); -         if(i<j) Swap (A[i], a[j]); -     } the      for(intD=0;(1&LT;&LT;D) <n;d++){ -         intm=1<<d,m2=m<<1, _w=t==1?G[d]:ng[d]; -          for(intI=0; i<n;i+=m2) for(intw=1, j=0; j<m;j++){ -             int&a=a[i+j+m],&b=a[i+j],t= (LL) w*a%P; +A=b-t;if(a<0) a+=P; -B=b+t;if(b>=p) b-=P; +w= (LL) w*_w%P; A         } at     } -     if(t==-1) for(intI=0, j=inv[n];i<n;i++) a[i]= (LL) a[i]*j%P; - } - //given a, find the inverse of a B - voidGETINV (int*a,int*b,intN) { -     if(n==1) {b[0]=pow (a[0],p-2);return;} inGETINV (a,b,n>>1); -     intk=n<<1, I; to      for(i=0; i<n;i++) tmp[i]=A[i]; +      for(i=n;i<k;i++) tmp[i]=b[i]=0; -NTT (Tmp,k,1), NTT (B,k,1); the      for(i=0; i<k;i++){ *b[i]= (LL) b[i]* (2-(LL) tmp[i]*b[i]%p)%P; $     if(b[i]<0) b[i]+=P;Panax Notoginseng     } -NTT (b,k,-1); the      for(i=n;i<k;i++) b[i]=0; + } A intMain () { the      for(G[k]=pow (g, (p1)/N), Ng[k]=pow (g[k],p-2), i=k-1; ~i;i--) +g[i]= (LL) g[i+1]*g[i+1]%p,ng[i]= (LL) ng[i+1]*ng[i+1]%P; -      for(inv[1]=1, i=2; i<=n;i++) inv[i]= (LL) (p-inv[p%i]) * (p/i)%p;inv2=inv[2]; $     intLen =1; $      while(Len <=100000) Len <<=1; -a[0] =1; -      for(inti =1; i < Len; i++) theA[i] = (LL) i*a[i-1]%P; - Getinv (A, b, Len);Wuyi      for(inti =0; i < Len; i++) theB[i] = (-b[i]+p)%P; -b[0]++; Wu     intT, N; scanf"%d", &t); -      while(t--){ Aboutscanf"%d", &n); $printf"%d\n", B[n]); -     } -     return 0; -}
View Code

NTT "51nod" 1514 wonderful sequence

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.