Description
A sequence of length \(n\) is initially \(0\) , you need to find a permutation \(P\) of length \(n-1\ ) , according to \(1\) to \ The order of (n\) , each time \(P_i\) and \(P_i+1\) are dyed into \(1\) , the value of a permutation is the number of operations that all positions become \(1\) , find the value and title of all permutations
Solution
We find the number of solutions whose value is \(\lceil\frac{n}{2}\rceil\) to \(n-1\) , and then count the contribution separately. The
operation is at most \(i\) times The number of schemes is \(f[i]\)
exactly \(i\) times the scheme is \(f[i]-f[i-1]\)
and \(f[i]=C_{i-1} ^{n-1-i}\)
The specific meaning: It can be regarded as every time you can choose \(+1,+2\) , to find the number of schemes that constitute \(n-1\) , we first default all \( +1\) , the rest is to choose \(+0,+1\) , as long as there are \(n-1-i\) in the total \(i-1\) operations select \(+1 \) , you will be able to achieve the goal
#include<bits/stdc++.h>
using namespace std;
template<class T>void gi(T &x){
int f;char c;
for(f=1,c=getchar();c<'0'||c>'9';c=getchar())if(c=='-')f=-1;
for(x=0;c<='9'&&c>='0';c=getchar())x=x*10+(c&15);x*=f;
}
const int N=1e6+10,mod=1e9+7;
int Fac[N],inv[N],n,f[N];
inline int C(int n,int m){
return 1ll*Fac[n]*inv[m]%mod*inv[n-m]%mod;
}
int main(){
freopen("pp.in","r",stdin);
freopen("pp.out","w",stdout);
cin>>n;
int ans=0,li=(n+1)/2;
Fac[0]=inv[0]=inv[1]=1;
for(int i=1;i<=n;i++)Fac[i]=1ll*Fac[i-1]*i%mod;
for(int i=2;i<=n;i++)inv[i]=(mod-1ll*(mod/i)*inv[mod%i]%mod)%mod;
for(int i=2;i<=n;i++)inv[i]=1ll*inv[i]*inv[i-1]%mod;
for(int i=li;i<n;i++)f[i]=1ll*C(i-1,n-1-i)*Fac[i]%mod*Fac[n-1-i]%mod;
for(int i=n-1;i>=li;i--)f[i]=(f[i]-f[i-1]+mod)%mod;
for(int i=n-1;i>=li;i--)ans=(ans+1ll*i*f[i])%mod;
cout<<ans<<endl;
return 0;
}