codeforces 1312d count the arrays(组合数学)

题目大意:

已知区间长度为n,现在按照规定,我们从[1,m]中选择一部分数字放入这个区间。

规定:必须有一个数字是重复的。

存在下标i,使得下标小于i的数字递增,下标大于i的数字递减,问我们总共有多少种不同的方法来放这些数字。

n,m<=1e5.

解题思路:

首先,我们可以想到C_m^{n-1} 从中选出n-1个数字,然后我们可以让其中一部分的数字重复,但是最大值不能够重复,否则破坏了约束。接着我们可能想满足递增递减约束,但是却下不来手,但是我们换一个角度来考虑,假设中间最大值放好了,重复数字放好了,剩下的n-3个数字是不是既可以放左边又可以放右边呢。所以还有pow(2,n-3)种可能。

所以答案是:

C_m^{n-1} * (n-2)*2^{n-3}

还有两个细节,分别是n=2还有n-1>m时我们需要输出0.

#include <bits/stdc++.h>
#define int long long 

using namespace std;
const int MAXN=2e5+10;
const int MODN=998244353;
int quick_pow(int a,int b){
    int ret=1;
    while(b){
        if(b&1)ret*=a;
        ret%=MODN;
        a*=a;
        a%=MODN;
        b>>=1;
    }
    return ret;
}
int32_t main(){
    int n,m;cin>>n>>m;
    if(n-1>m || n==2)cout<<0<<endl;
    else{
        vector<int> fact(MAXN,1);
        for(int i=1;i<MAXN;i++){
            fact[i]=fact[i-1]*i;
            fact[i]%=MODN;
        }
        // cerr<<fact[n-1]<<endl;
        int comb=fact[m]*quick_pow((fact[n-1]*fact[m-(n-1)])%MODN,MODN-2);
        // cerr<<comb<<endl;
        comb%=MODN;
        comb*=(n-2);
        comb%=MODN;
        comb*=quick_pow(2,n-3);
        comb%=MODN;
        cout<<comb<<endl;
    }
	return 0;
}
发布了171 篇原创文章 · 获赞 4 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/FrostMonarch/article/details/104815461