Count the Arrays

Problem - 1312D - Codeforces

求构造数列数量

数列满足条件

1.n个元素

2.元素范围(1-m)

3.仅存在一对索引,元素相同

4.仅存在一个峰 (ai<ai+1>ai+2)

一. 应该是m中选n-1个 即\binom{m}{n-1}

二.存在一个峰,所以选择元素值应该是排除最大的那个 (n-2)

三.选择的元素位置可以在左边也可以在右边,我们确定了最大的在中间,相同的在左右两边,因此有n-3个元素是不确定的,2^{n-3}

根据乘法原理

数量为\binom{m}{n-1}*(n-2)*2^{n-3}

// Problem: D. Count the Arrays
// Contest: Codeforces - Educational Codeforces Round 83 (Rated for Div. 2)
// URL: https://codeforces.com/problemset/problem/1312/D
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<iostream>
using namespace std;
typedef long long ll;
const int mod=998244353;
const int N=2e5+9;
ll f[N],invf[N];
ll qmi(ll a,ll b){
	ll res=1;
	while(b){
		if(b&1){
			res=res*a%mod;
		}
		b>>=1;
		a=a*a%mod;
	}
	return res;
}
ll inv(ll x){
	return qmi(x,mod-2)%mod;
}
ll C(ll n,ll m){
	return ((f[n]*invf[m]%mod)*invf[n-m])%mod;
}
void init(){
	f[0]=1;
	for(int i=1;i<=200002;i++){
		 f[i]=f[i-1]*i%mod;
	}
	invf[200001]=inv(f[200001]);
	for(int i=200000;i>=1;i--){
		invf[i]=invf[i+1]*(i+1)%mod;
	}
}
// C(m,n-1)*(n-2)*qmi(2,n-3);
int main(){
	init();
	int n,m;
	cin>>n>>m;
	if(n==2){
		cout<<0<<'\n';
		return 0;
	}
	ll t1=C(m,n-1);
	ll t2=n-2;
	ll t3=qmi(2,n-3);
	ll ans=1;
	ans*=t1;
	ans*=t2;
	ans%=mod;
	ans*=t3;
	cout<<ans%mod<<'\n';
	return 0;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值