题意

给你\(n\)和\(m\),问满足以下条件的数列的个数:

  • 数列长度为\(n\)
  • 数列值域范围为\(\left[1,m\right]\)
  • 数列有且仅有一对相等的数
  • 数列是单峰数列(先严格递增后严格递减,严格递增或严格递减)

解题思路

首先从\(m\)元素中挑出\(n-1\)个不同的值,有\(C_m^{n-1}\)种方法。现在数列的值域就可以只看成\(\left[1,n-1\right]\)了。

然后这\(n-1\)个元素中,先放置好\(n-1\),假设重复元素的值为\(i(i\in\left[1,n-2\right])\)。那么这3个元素的位置只有一种放置方法符合条件。还剩下\(n-3\)个元素,这些元素既可以在峰的左侧,也可以在峰的右侧,且对于所有分法都有且只有一种放置方法,所以有\(2^{n-3}\)种方法。最后乘上\(i\)的取值方法数也就是\(n-2\),结果如下:

\[Ans=(n-2)C_m^{n-1}2^{n-3}
\]

AC代码

#include <bits/stdc++.h>
using namespace std; typedef long long ll;
typedef pair<int,int> pi; #define x first
#define y second #define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define endl '\n' const double PI=acos(-1.0); mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l,int r){return l+rng()%(r-l+1);} namespace IO{
bool REOF = 1; //为0表示文件结尾
inline char nc() {
static char buf[100000], *p1 = buf, *p2 = buf;
return p1 == p2 && REOF && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2) ? (REOF = 0, EOF) : *p1++;
} template<class T>
inline bool read(T &x) {
char c = nc();bool f = 0; x = 0;
while (c<'0' || c>'9')c == '-' && (f = 1), c = nc();
while (c >= '0'&&c <= '9')x = (x << 3) + (x << 1) + (c ^ 48), c = nc();
if(f)x=-x;
return REOF;
} template<typename T, typename... T2>
inline bool read(T &x, T2 &... rest) {
read(x);
return read(rest...);
} inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')); }
// inline bool need(char &c) { return ((c >= 'a') && (c <= 'z')) || ((c >= '0') && (c <= '9')) || ((c >= 'A') && (c <= 'Z')) || c==' '; } inline bool read_str(char *a) {
while ((*a = nc()) && need(*a) && REOF)++a; *a = '\0';
return REOF;
} inline bool read_dbl(double &x){
bool f = 0; char ch = nc(); x = 0;
while(ch<'0'||ch>'9') {f|=(ch=='-');ch=nc();}
while(ch>='0'&&ch<='9'){x=x*10.0+(ch^48);ch=nc();}
if(ch == '.') {
double tmp = 1; ch = nc();
while(ch>='0'&&ch<='9'){tmp=tmp/10.0;x=x+tmp*(ch^48);ch=nc();}
}
if(f)x=-x;
return REOF;
} template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; } template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
while(*sdbg!=',')cerr<<*sdbg++;
cerr<<'='<<h<<','<<' '; _dbg(sdbg+1, a...);
} template<class T> ostream &operator<<(ostream& os, vector<T> V) {
os << "["; for (auto vv : V) os << vv << ","; return os << "]";
} template<class T> ostream &operator<<(ostream& os, set<T> V) {
os << "["; for (auto vv : V) os << vv << ","; return os << "]";
} template<class T> ostream &operator<<(ostream& os, map<T,T> V) {
os << "["; for (auto vv : V) os << vv << ","; return os << "]";
} template<class L, class R> ostream &operator<<(ostream &os, pair<L,R> P) {
return os << "(" << P.st << "," << P.nd << ")";
} #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
} using namespace IO;
const int maxn=2e5+5;
const int maxv=2e5+5;
const int mod=998244353; // 998244353 1e9+7
const int INF=1e9+7; // 1e9+7 0x3f3f3f3f 0x3f3f3f3f3f3f3f3f
const double eps=1e-12; int dx[4]={0,1,0,-1};
//int dx[8]={1,0,-1,1,-1,1,0,-1};
int dy[4]={1,0,-1,0};
//int dy[8]={1,1,1,0,0,-1,-1,-1}; // #define ls (x<<1)
// #define rs (x<<1|1)
// #define mid ((l+r)>>1)
// #define lson ls,l,mid
// #define rson rs,mid+1,r // int tot,head[maxn];
// struct Edge{
// int v,nxt;
// Edge(){}
// Edge(int _v,int _nxt):v(_v),nxt(_nxt){}
// }e[maxn<<1];
// void init(){
// tot=1;
// memset(head,0,sizeof(head));
// }
// void addedge(int u,int v){
// e[tot]=Edge(v,head[u]); head[u]=tot++;
// e[tot]=Edge(u,head[v]); head[v]=tot++;
// }
// void addarc(int u,int v){
// e[tot]=Edge(v,head[u]); head[u]=tot++;
// } /**
* ********** Backlight **********
* 仔细读题
* 注意边界条件
* 记得注释输入流重定向
* 没有思路就试试逆向思维
* 加油,奥利给
*/
ll n,m;
ll qp(ll a,ll b){
ll res=1;
while(b){
if(b&1)res=res*a%mod;
a=a*a%mod;
b>>=1;
}
return res;
} ll inv(ll x){return qp(x,mod-2);} void solve(){
read(n,m);
ll ans=n-2;
for(int i=1;i<=m;i++)ans=ans*i%mod;
for(int i=1;i<=n-1;i++)ans=ans*inv(i)%mod;
for(int i=1;i<=m-n+1;i++)ans=ans*inv(i)%mod;
ans=ans*qp(2,n-2)%mod;
ans=ans*inv(2)%mod;
printf("%lld\n",ans);
} int main()
{
// freopen("in.txt","r",stdin);
// ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
// int _T; read(_T); for(int _=1;_<=_T;_++)solve();
// while(read(n))solve();
solve();
return 0;
}

最新文章

  1. Eclipse启动Tomcat时发生java.lang.IllegalArgumentException: &lt;session-config&gt; element is limited to 1 occurrence
  2. 单片机与控制实验(2)——LED点阵显示屏
  3. 《Linux内核分析》第六周 进程的描述与创建
  4. 仿知乎Android端回答UI
  5. MZhong&#39;s Cover Letter
  6. java文件过滤器
  7. python设计模式之观察者模式
  8. 浅谈OpenStack架构
  9. JAVA_SE基础——43.抽象类
  10. java获取当前系统时间
  11. TensorFlow windows 安装(base anaconda)
  12. Net Core Docker 容器部署,修改,保存
  13. Linux下间隔多少秒 (即以秒为单位) 去执行某条命令或某个shell脚本的操作方法【转】
  14. springboot之jar包部署步骤
  15. 005 Spark快速入门的简单程序案例
  16. Nginx80端口转发+域名——实现IP+端口隐藏
  17. MyEclipse如何恢复删掉的文件
  18. SpringMVC由浅入深day01_4DispatcherSerlvet.properties
  19. C/C++UNION中包含STRUCT
  20. spring配置bean的生命周期

热门文章

  1. 借助GPU Boost和K80 Autoboost提高性能
  2. 前端面试基础题:Ajax原理
  3. AI测温落地趋势:已成日常刚需 产品形态呈细分化发展
  4. Python使用Tornado+Redis维护ADSL拨号服务器代理池
  5. Docker 快速搭建 MySQL 5.6 开发环境
  6. Scss 定义内层class的简单写法
  7. Android小技巧总结——持续更新
  8. CSS动画实例:跳跃的字符
  9. java多线程:线程同步synchronized(不同步的问题、队列与锁),死锁的产生和解决
  10. mycli工具mysql命令自动补全