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

D. Count the Arrays
time limit per test2 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard output
Your task is to calculate the number of arrays such that:

each array contains n elements;
each element is an integer from 1 to m;
for each array, there is exactly one pair of equal elements;
for each array a, there exists an index i such that the array is strictly ascending before the i-th element and strictly descending after it (formally, it means that aj<aj+1, if j<i, and aj>aj+1, if j≥i).
Input
The first line contains two integers n and m (2≤n≤m≤2⋅105).

Output
Print one integer — the number of arrays that meet all of the aforementioned conditions, taken modulo 998244353.

Examples
inputCopy
3 4
outputCopy
6
inputCopy
3 5
outputCopy
10
inputCopy
42 1337
outputCopy
806066790
inputCopy
100000 200000
outputCopy
707899035
Note
The arrays in the first example are:

[1,2,1];
[1,3,1];
[1,4,1];
[2,3,2];
[2,4,2];
[3,4,3].

题意:给了1~m之间的数字,让构造一个长度为n的序列
这个序列必须有一个数字重复 也就是说必须有n-1种数字
对于位置i 前面必须单调递增 后面必须单调递减 求种类数

思路
m种数字选n-1种 第一步自然就是组合数先求出来C(m,n-1)
然后 我们考虑 重复的数字
因为峰值只能有一个 那么重复的数字一定不是最大值, 所以重复的数字的选择有n-2种
那么对于种类确定了之后,根据题意可以推断出来,重复的数字一定是在峰值的左右个一个,那么去掉这三个数后,还有n-3个数字 并且种类两两不同
对于这n-3个数 也可以说n-3种数,要么他在峰值的左边 要么在右边
其实就是2^(n-3) 种分配方式 不是左就是右 类比二进制位不是0就是1
所以答案就是C(m,n-1)*(n-2)*2^(n-3)
注意特判n==2的时候 因为这时候要选取两个 又因为题中要求要有一对一样数字 这显然不满足峰值那个要求

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
ll n,m;
ll qpow(ll a,ll b)
{
    ll ans=1;
    a%=mod;
    while(b)
    {
        if(b&1)
            ans=ans*a%mod;
        b>>=1;
        a=a*a%mod;
    }
    return ans;
}
int main()
{


    scanf("%lld%lld",&n,&m);
    if(n==2) return puts("0"),0;
    ll an=1,res=1;
    n--;
    ll nn=n;
    if(nn*2>m) nn=m-nn;
    for(int i=0; i<nn; i++)
    {
        an=an*(m-i)%mod;
        res=res*(nn-i)%mod;
    }
    an=an%mod*qpow(res,mod-2)%mod;
    an=an*(n-1)%mod*qpow(2ll,n-2)%mod;
    printf("%lld\n",an);

    return 0;
}

全部评论

相关推荐

🎓学历背景:双非土木硕👨‍💻意向职位:AI应用开发大佬们可以帮我看看简历吗,秋招至今0offer
秋招结束再玩瓦:今年科班都不好找哇……你可以试试交叉岗,比如制造业国企的一些开发算法,或者互联网的边缘岗,it技术支持,运维这些
我的简历长这样
点赞 评论 收藏
分享
10-20 15:26
门头沟学院 Java
桥头牛油火锅:这个比例不正常,简历的话项目经历放中间,项目功能分点可以再明确点,前面加“·”或者“1 2 3”,另外简历上的照片可以去外面摄影店拍一下,以后也会用到的,hr筛人也是多少会看的,毕竟世界是一个巨大的卡颜局嘛,还有有些hr由于消息太多可能没看到,后面可能会回来找你,要简历的还会多一点,我也是普2本,比例大致是600:90:15:3,当然我实力不太够,拿的offer比较少,慢慢来吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务