Educational Codeforces Round 83 (Rated for Div. 2) D - Count the Arrays

D - Count the Arrays
题意:
m个数排成先严格单调递增再严格单调递减 求有多少种排列方式,答案对 998244353 998244353 取模
思路:
组合数学 l u c a s lucas 定理
m m 个数中取出 n 1 n-1 个数,其中一个数为最大的数,他是不能有2个的,所以能从剩下的 n 2 n-2 个数中选一个数放到最大数的另一边。
我做的时候想不通如何表达顺序,这个 q p o w ( 2 , n 3 ) qpow(2,n-3) 就代表顺序,可以这么理解,最大的数的位置一定是在中间的无法改变,两个一样的数位置无法改变,最大的数左边一个右边一个,而剩下 n 3 n-3 个数的位置是可以选左边和右边有两种选法,那一共乘起来就有 q p o w ( 2 , n 3 ) qpow(2,n-3) 种选法。
a n s = ( n 2 ) q p o w ( 2 , n 3 ) C ( m , n 1 ) ans=(n-2)*qpow(2,n-3)*C(m,n-1)
坑点:
因为两个数相乘可能会爆ll为负数所以要及时的 % m o d \%mod
最后就变成了
a n s = ( n 2 ) q p o w ( 2 , n 3 ) % m o d C ( m , n 1 ) % m o d ans=(n-2)*qpow(2,n-3)\%mod*C(m,n-1)\%mod

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 998244353;
const ll  maxn = 1e6 + 5;

ll p=mod;
inline ll qpow(ll a,ll b){
	ll base=a%p;
	ll ans=1;
	while(b>0){
		if(b&1) ans=(ans*base)%p;
		base=base*base%p;
		b>>=1;
	}
	return ans%mod;
}

inline ll C(ll n,ll m){
	if(n<m) return 0;//组合数n<m特判
	if(m>n-m) m=n-m;//组合数性质
	ll a=1,b=1;
	for(int i=0;i<m;i++){
		a=a*(n-i)%p;//组合数分子 a 
		b=b*(i+1)%p;//组合数分母 b
	} 
	return a*qpow(b,p-2)%p;//费马小定理 a/b=a*b^(p-2) 
}

inline ll Lucas(ll n,ll m){
	if(m==0) return 1;
	return Lucas(n/p,m/p)*C(n%p,m%p)%p;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll n,m;
	cin>>n>>m;
	ll ans=(n-2)*qpow(2,n-3)%mod*C(m,n-1)%mod;
	cout<<ans;
	return 0;
}
发布了50 篇原创文章 · 获赞 9 · 访问量 6800

猜你喜欢

转载自blog.csdn.net/Meulsama/article/details/104766007