组合计数题

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3010, INF = 0x3f3f3f3f, mod = 1e9 + 7;
int C[N][N],a[N];
int n,m,q,t,cnt,ans;

void init()
{
    for(int i=0;i<N;i++)
    {
        for(int j=0;j<=i;j++)
        {
            if(!j) C[i][j]=1;
            else C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
        }
    }
    a[0]=1;
    for(int i=1;i<N;i++) a[i]=(a[i-1]*i)%mod;
}

void solve() 
{
    init();
    cin>>n>>m>>q>>t;
    if(n>t) cnt++;
    for(int i=1;i<=q;i++)
    {
        int x;
        cin>>x;
        if(x>t) cnt++;
    }

    if(n==t)
    {
        if(2*m<=q+1) cout<<C[2*m][1]*a[q]%mod<<endl;
        else cout<<a[q+1]%mod<<endl;
    }
    else
    {
        if(2*m<=q+1)
        {
            if(cnt<=m) cout<<C[2*m][1]*a[q]%mod<<endl;
            else
            {
                for(int i=0;i<=m-1;i++)
                {
                    for(int j=0;j<=q+1-cnt;j++)
                    {
                        if(i+j>2*m-1) break;
                        ans=(ans+C[cnt-1][i]*C[q+1-cnt][j]%mod*a[i+j]%mod*a[q-i-j]%mod)%mod;
                    }
                }
                cout<<ans<<endl;
            }
        }
        else
        {
            if(cnt<=m) cout<<a[q+1]%mod<<endl;
            else
            {
                for(int i=0;i<=m-1;i++)
                {
                    for(int j=0;j<=q+1-cnt;j++)
                    {
                        ans=(ans+C[cnt-1][i]*C[q+1-cnt][j]%mod*a[i+j]%mod*a[q-i-j]%mod)%mod;
                    }
                }
                cout<<ans<<endl;
            }
        }
    }
}


signed main()
{
    int T = 1;
    //scanf("%lld", &T);
    while (T--)
    {
        solve();
    }

    return 0;
}
相关面经
全部面经
招聘动态
更多动态