跳到正文

题解

题解:AT_abc439_f [ABC439F] Beautiful Kadomatsu

Love_Song约 7 分钟
原文
文章信息与版本

文章信息

收录
2026年1月4日
更新
2026年1月4日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

题意简述

给定一个长度为 NN 的排列 PP,统计 PP 的所有子序列中满足门松条件的个数,并对 998244353998244353 取模输出。

门松序列定义: 设序列 a=(a1,a2,…,ak)a = (a_1, a_2, \ldots, a_k),定义:

  • xx:满足 ai−1<ai>ai+1a_{i-1} < a_i > a_{i+1} 的 ii 的个数(2≤i≤k−12 \le i \le k-1)。
  • yy:满足 ai−1>ai<ai+1a_{i-1} > a_i < a_{i+1} 的 ii 的个数(2≤i≤k−12 \le i \le k-1)。

当且仅当 x>yx > y 时,称 aa 是门松的。

输出满足条件的子序列个数对 998244353998244353 取模的结果。

思路

可以观察到对于任意长度 k≥3k \ge 3 的序列 a=(a1,a2,…,ak)a = (a_1, a_2, \ldots, a_k),其峰谷数量差 x−yx - y 完全由序列的首尾趋势决定:

  1. “上升” 开始,“上升” 结束(模式:↗⋯↗\nearrow \cdots \nearrow)

    • 特征:a1<a2a_1 < a_2 且 ak−1<aka_{k-1} < a_k。
    • 结果:x=yx = y。
  2. “下降” 开始,“下降” 结束(模式:↘⋯↘\searrow \cdots \searrow)

    • 特征:a1>a2a_1 > a_2 且 ak−1>aka_{k-1} > a_k。
    • 结果:x=yx = y。
  3. “下降” 开始,“上升” 结束(模式:↘⋯↗\searrow \cdots \nearrow)

    • 特征:a1>a2a_1 > a_2 且 ak−1<aka_{k-1} < a_k。
    • 结果:x=y−1x = y - 1(即 y>xy > x)。
  4. “上升” 开始,“下降” 结束(模式:↗⋯↘\nearrow \cdots \searrow)

    • 特征:a1<a2a_1 < a_2 且 ak−1>aka_{k-1} > a_k。
    • 结果:x=y+1x = y + 1(即 x>yx > y)。

所以我们可得出序列 aa 是门松序列(x>yx > y)的充要条件:

{a1<a2ak−1>ak\begin{cases} a_1 < a_2 \\ a_{k-1} > a_k \end{cases}

这就将复杂的峰谷计数问题转化为简单的首尾条件判断:

  • 原问题:统计所有 x>yx > y 的子序列。
  • 转化后:统计所有满足 a1<a2a_1 < a_2 且 ak−1>aka_{k-1} > a_k 的子序列。

做法

设子序列在原排列中的位置为 i1,i2,…,iki_1, i_2, \ldots, i_k,关注两个关键位置:

  • a2a_2 的位置:记为 ll。
  • ak−1a_{k-1} 的位置:记为 rr。

情况 A:l=rl = r(长度 = 3)

此时子序列为 (a1,a2,a3)(a_1, a_2, a_3),要求 a1<a2>a3a_1 < a_2 > a_3。

令:

  • pre[l]pre[l] = ll 左边比 P[l]P[l] 小的数的个数。
  • suf[l]suf[l] = ll 右边比 P[l]P[l] 小的数的个数。

贡献:ansA=∑l=1npre[l]×suf[l]\text{ans}_A = \sum_{l=1}^n pre[l] \times suf[l]。

情况 B:l<rl < r(长度 ≥ 4)

  • a1a_1 的选择:pre[l]pre[l] 种。
  • aka_k 的选择:suf[r]suf[r] 种。
  • 中间元素选择:2r−l−12^{r-l-1} 种。

贡献:ansB=∑l=1n−1∑r=l+1npre[l]×suf[r]×2r−l−1\text{ans}_B = \sum_{l=1}^{n-1} \sum_{r=l+1}^n pre[l] \times suf[r] \times 2^{r-l-1}。

公式变形:

ansB=∑l=1n−1pre[l]×2−(l+1)×∑r=l+1n(suf[r]×2r)=∑l=1n−1pre[l]×inv[l+1]×sum[l+1]\begin{aligned} \text{ans}_B &= \sum_{l=1}^{n-1} pre[l] \times 2^{-(l+1)} \times \sum_{r=l+1}^n (suf[r] \times 2^r) \\ &= \sum_{l=1}^{n-1} pre[l] \times inv[l+1] \times sum[l+1] \end{aligned}

其中:

  • pow2[i]=2i mod Mpow2[i] = 2^i \bmod M。
  • inv[i]=2−i mod Minv[i] = 2^{-i} \bmod M(预计算逆元)。
  • sum[i]=∑r=in(suf[r]×pow2[r])sum[i] = \sum_{r=i}^n (suf[r] \times pow2[r])(后缀和)。

时间复杂度:O(Nlog⁡N)O(N \log N)。

Code:

C++
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N=3e5+5;
const int M=998244353;
int n,p[N],pre[N],suf[N],sum[N],pow2[N],inv[N];
struct BIT{
	int c[N],n;
	inline void init(int k){
		n=k;
		memset(c,0,sizeof c);
	}
	inline int lowbit(int x){
		return x&(-x);
	}
	inline void update(int x,int d){
		for(;x<=n;x+=lowbit(x)){
			c[x]+=d;
		}
	}
	inline int ask(int x){
		int res=0;
		for(;x;x-=lowbit(x)){
			res+=c[x];
		}
		return res;
	}
}bit;
int qpow(int a,int b){
	int res=1;
	while(b){
		if(b&1)res=res*a%M;
		a=a*a%M;
		b>>=1;
	}
	return res;
}
signed main(){
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>p[i];
	}
	//预处理幂和逆元
	pow2[0]=inv[0]=1;
	int inv2=qpow(2,M-2);
	for(int i=1;i<=n;i++){
		pow2[i]=pow2[i-1]*2%M;
		inv[i]=inv[i-1]*inv2%M;
	}
	//计算pre数组:每个位置左边小于它的数的个数
	bit.init(n);
	for(int i=1;i<=n;i++){
		pre[i]=bit.ask(p[i]-1);
		bit.update(p[i],1);
	}
	//计算suf数组:每个位置右边小于它的数的个数
	bit.init(n);
	for(int i=n;i>=1;i--){
		suf[i]=bit.ask(p[i]-1);
		bit.update(p[i],1);
	}
	//计算后缀和:sum[i] = Σ_{r=i}^n (suf[r] * 2^r)
	for(int i=n;i>=1;i--){
		sum[i]=(sum[i+1]+suf[i]*pow2[i]%M)%M;
	}
	int ans=0;
	for(int l=1;l<=n;l++){
		//长度为3的情况 (l == r)
		ans=(ans+pre[l]*suf[l]%M)%M;
		//长度大于3的情况 (l < r),计算 pre[l] * Σ_{r=l+1}^n (suf[r] * 2^(r-l-1))
		ans=(ans+pre[l]*sum[l+1]%M*inv[l+1]%M)%M;
	}
	cout<<ans;
}
原文