BZOJ4517: [Sdoi2016] permutation count

Source: Internet
Author: User

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
60695423first enumerate what numbers satisfy the ai=i, then it becomes the classic error-line problem. The recursive f[n]= (F[n-1]+f[n-2]) * (n-1) of the wrong row problem. We consider the number of the smallest person in the n person I, set him up to position J. 1. If the J person stands at position I, the problem turns into a n-2 problem. 2. If the J person does not stand in the I position, then except the I number of people all stand wrong, the problem turns into a n-1 problem of the wrong row. preprocessing factorial is the inverse element.
#include <cstdio> #include <cctype> #include <queue> #include <cstring> #include <algorithm > #define REP (i,s,t) for (int. i=s;i<=t;i++) #define DWN (I,S,T) for (int. i=s;i>=t;i--) #define REN for (int i=first[x ];i;i=next[i]) using namespace Std;const int Buffersize=1<<16;char Buffer[buffersize],*head,*tail;inline Char Getchar () {if (head==tail) {int l=fread (Buffer,1,buffersize,stdin); tail= (Head=buffer) +l;} return *head++;}    inline int read () {int X=0,f=1;char c=getchar (); for (;!    IsDigit (c); C=getchar ()) if (c== '-') f=-1;    for (; IsDigit (c); C=getchar ()) x=x*10+c-' 0 '; return x*f;} typedef long LONG ll;const int maxn=1000010;const int mod=1000000007;int xp[maxn],inv[maxn],f[maxn];int C (int n,int m) {RE Turn (ll) Xp[n]*inv[m]%mod*inv[n-m]%mod;} void init (int n) {xp[0]=inv[0]=inv[1]=1;rep (i,1,n) xp[i]= (LL) xp[i-1]*i%mod;rep (i,2,n) inv[i]= (LL) inv[mod%i]* ( mod-mod/i)%mod;rep (i,1,n) inv[i]= (LL) inv[i-1]*inv[i]%mod;f[2]=f[0]=1;rep (i,3,n) f[i]= (LL) (F[i-1]+f[i-2]) * (i-1)% MoD;} InchT A[maxn],b[maxn];int Main () {int n=read (), M=0;rep (i,1,n) M=max (M,a[i]=read ()), B[i]=read (), Init (m), Rep (i,1,n) {if (b[ I]>a[i]) puts ("0"), Else printf ("%d\n", (LL) C (A[i],b[i]) *f[a[i]-b[i]]%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.