[ARC154E] Reverse and Inversion
[ARC154E] Reverse and Inversion#
先化简,考虑每一个 的贡献,它等于
因为
所以原式为
现在的关键就是求出,因为对于每一种方案的值都要求出,相当于求出其期望值。
考虑 期望被交换到哪个位置,假设 一次操作后 被交换到 ,其概率为 ,观察发现 交换到 的概率和其是一样的,所以如果 被操作过,其期望位置为 ,否则为位置为 。
易得位置 没被操作的概率为 ,那么 的期望值为 。
Code
#include<cstdio>
#include<iostream>
#define IN inline
#define LL long long
using namespace std;
const int N = 2e5 + 5, P = 998244353;
LL a[N], p[N], n, m;
IN int read() {
int t = 0,res = 0; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) t |= (ch == '-');
for (; isdigit(ch); ch = getchar()) res = (res << 3) + (res << 1) + (ch ^ 48);
return t ? -res : res;
}
LL fpow(LL x, LL y) {
LL res = 1; y = y % P;
for (; x; x >>= 1, y = y * y % P)
if (x & 1) res = res * y % P;
return res;
}
int main() {
n = read(), m = read();
for (int i = 1; i <= n; i++) a[i] = read();
LL inv = fpow(P - 2, n * (n + 1) / 2), iv2 = fpow(P - 2, 2LL);
for (LL i = 1; i <= n; i++) p[i] = fpow(m, (P + 1 - (LL)i * (n - i + 1) % P * inv % P) % P);
LL ans = 0;
for (LL i = 1; i <= n; i++)
(ans += P + i * i % P - a[i] * (p[i] * i % P + (1 + P - p[i]) * (n + 1) % P * iv2 % P) % P) %= P;
printf("%lld\n", ans * fpow(m, n * (n + 1) / 2) % P);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通