Make a make

Subject:
https://ac.nowcoder.com/acm/problem/15077

Every time you can push a new element or pop an element when the stack is not empty, nnn elements must be stacked sequentially, andWYF WYFW Y F hope that themmAfter m elements are put on the stack, there happens to bekk in thestackk elements, and now he wants to know how many stacking and unstacking orders satisfy this condition.

Idea:
First divide into two sub-problems

  • In mmBefore m elements are put on the stack, there arem − 1 m-1m1 stack,m − k mkmk pops, and the number of pushes at each moment is not less than the number of pops, the plan is
    (2 m − 1 − km − 1) km {2m-1-k\choose m-1}\frac {k}{m}(m12 m1k)mk
  • In mmAfter m elements are stacked, there aren − m nmnm into the stack,n − m + k n-m+knm+k pops, and the number of pops minus the number of pushes at each moment is less than or equal tokkk (there is alreadykk in thestackk elements), the number of schemes is
    (2 n − 2 m + kn − m) k + 1 n − m + k + 1 {2n-2m+k\choose nm}\frac{k+1}{n-m +k+1}(nm2 n2 m+k)nm+k+1k+1
    The answer is multiplication

Note: There are illegal situations in calculating the number of combinations

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
const int N=4000009;
int T;
ll p[N],p1[N];
ll qpow(ll a, ll b){
    
    
    ll res=1;
    a%=mod;
    while(b){
    
    
        if(b&1)
            res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res;
}
ll cal(ll a,ll b){
    
    
    if(a<0||b<0||a-b<0)//不合法
        return 0;
    return p[a]*p1[b]%mod*p1[a-b]%mod;
}
int main() {
    
    
    p[0]=1;
    for(int i=1;i<N;i++)
        p[i]=p[i-1]*i%mod;
    p1[N-1]=qpow(p[N-1],mod-2);
    for(int i=N-2;i>=0;i--)
    p1[i]=p1[i+1]*(i+1)%mod;
    cin>>T;
    while(T--){
    
    
        ll n,m,k,tmp,tmp1;
        cin>>n>>m>>k;
        tmp=cal(2*m-1-k,m-1)*k%mod*qpow(m,mod-2)%mod;
        tmp1=cal(2*n-2*m+k,n-m)*(k+1)%mod*qpow(n-m+k+1,mod-2)%mod;
        cout<<tmp*tmp1%mod<<endl;
    }
    return 0;
}

Guess you like

Origin blog.csdn.net/qq_43520313/article/details/109211081
Recommended