AtCoder Regular Contest 116 B - Products of Min-Max(排列组合)

Problem Statement

Given is a sequence A of N integers. There are 2N−1 non-empty subsequences B of A. Find the sum of max(B)×min(B) over all of them.

Since the answer can be enormous, report it modulo 998244353.

Constraints

  • All values in input are integers.
  • 1≤N≤2×105
  • 0≤Ai≤998244352

和2021牛客寒假训练赛某题很类似。暴力肯定不可做,考虑枚举贡献。首先对序列排序,朴素的想法是枚举最大值a[j]和最小值a[i],乘起来再乘以以此为最大值最小值的序列个数(即2ji1),注意如果j == i 或者j == i + 1的话个数只有一个。这样也是会t的。不妨进一步考虑,只枚举最大值,然后乘以一个类似前缀和的东西,比如枚举到的最大值是a[4],那么此时答案要加上的就是a[4]×(a[1]×22+a[2]×21+a[3]×20+a[4]×20),如果枚举到的是a[5],则为a[5]×(a[1]×23+a[2]×22+a[3]×21+a[4]×20+a[5]×20)。不难发现规律:不妨设i = 4时类前缀和tmp为a[1]×22+a[2]×21+a[3]×20,当i = 5时tmp先变为2倍,然后再加上a[i1]×20,此时令ans+=tmp+a[i]×a[i]即可。注意特判i = 1,2,3的情况。

#include <iostream>
#define mod 998244353
using namespace std;
int n;
long long a[2000005], ans = 0;
int main() {
	cin >> n;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	sort(a + 1, a + n + 1);
	long long tmp = 0;
	for(int i = 1; i <= n; i++) {
		if(i == 1) {
			tmp = a[1];
			ans += a[1] * a[1] % mod;
		} else if(i == 2) {
			tmp = (a[1] + a[2]) % mod;
			ans = (ans + tmp * a[2] % mod) % mod;
		} else if(i == 3) {
			tmp = (2 * a[1] % mod + a[2] % mod) % mod;
			ans = (ans + tmp * a[3] % mod) % mod;
			ans = (ans + a[3] * a[3] % mod) % mod; 
		} else {
			tmp = tmp * 2 % mod;
			tmp = (tmp + a[i - 1]) % mod;
			ans = (ans + tmp * a[i] % mod) % mod;
			ans = (ans + a[i] * a[i] % mod) % mod;
		}
	}
	cout << ans;
	return 0;
}
posted @   脂环  阅读(118)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
历史上的今天:
2020-03-29 归并排序模板
2020-03-29 Codeforces 627 E. Sleeping Schedule(线性DP)
2020-03-29 Codeforces Round #627 (Div. 3) D. Pair of Topics(二分/直接遍历)
2020-03-29 codeforces 1324 C. Frog Jumps(贪心/二分)
2020-03-29 AtCoder Beginner Contest 053 D - Card Eater(思维)
点击右上角即可分享
微信分享提示
主题色彩