[题解]AT_arc116_b [ARC116B] Products of Min-Max
思路
我们容易可以得到一个朴素的做法,首先对 数组排序,然后枚举最大值和最小值 ,那么对于中间的元素都有选与不选两种情况,得到答案:
然后对这个式子做一个化简:
发现对于每一个 , 都是类似的,所以考虑预处理。
定义 ,那么发现:
然后,发现对于每一项 对于原式都多乘了一个 ,直接除掉即可。得答案为:
时间复杂度 。
Code
#include <bits/stdc++.h>
#define int long long
#define re register
using namespace std;
const int N = 2e5 + 10,mod = 998244353;
int n,ans;
int arr[N],pot[N],mul[N],inv[N];
inline int read(){
int r = 0,w = 1;
char c = getchar();
while (c < '0' || c > '9'){
if (c == '-') w = -1;
c = getchar();
}
while (c >= '0' && c <= '9'){
r = (r << 3) + (r << 1) + (c ^ 48);
c = getchar();
}
return r * w;
}
inline int Add(int a,int b){
return (a + b) % mod;
}
inline int Sub(int a,int b){
return ((a - b) % mod + mod) % mod;
}
inline int Mul(int a,int b){
return a * b % mod;
}
inline void exgcd(int a,int b,int &x,int &y){
if (!b){
x = 1;
y = 0;
return;
}
exgcd(b,a % b,y,x);
y = y - a / b * x;
}
inline int get_inv(int a,int p){
int x,y;
exgcd(a,p,x,y);
return (x % mod + mod) % mod;
}
inline void init(){
pot[0] = 1;
for (re int i = 1;i <= n + 1;i++){
pot[i] = Mul(pot[i - 1],2);
mul[i] = Add(mul[i - 1],Mul(arr[i],pot[i]));
inv[i] = get_inv(pot[i],mod);
}
}
signed main(){
n = read();
for (re int i = 1;i <= n;i++) arr[i] = read();
sort(arr + 1,arr + n + 1);
init();
for (re int i = 1;i <= n;i++){
ans = Add(ans,Mul(Mul(Sub(mul[n],mul[i]),inv[i + 1]),arr[i]));
ans = Add(ans,Mul(arr[i],arr[i]));
}
printf("%lld",ans);
return 0;
}
作者:WaterSun
出处:https://www.cnblogs.com/WaterSun/p/18263293
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
分类:
题解
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 张高兴的大模型开发实战:(一)使用 Selenium 进行网页爬虫
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构