题解:AT_arc154_e [ARC154E] Reverse and Inversion

· · 题解

题目传送门

思路

考虑一下对于一个排列 p,f(p) 长什么样:

\sum_{i=1}^n \sum_{j=i+1}^n (j-i) [p_i>p_j]\\ =\sum_{i=1}^n i \left({\sum_{j=1}^{i-1}[p_i<p_j] -\sum_{j=i+1}^n [p_i>p_j]}\right) \\ =\sum_{i=1}^n i \left({i-1-\sum_{j=1}^{i-1}[p_i>p_j] -\sum_{j=i+1}^n [p_i>p_j]}\right) \\ =\sum_{i=1}^n i(i-p_i)

然后你感觉直接对这个计数很没有前途,考虑拆开每一位算期望,令 q_i 为原来的第 i 个点的期望位置,则答案为 \frac{n \times (n+1)}{2} \sum_{i=1}^n i^2-q_i \times p_i。

注意到如果只考虑包含 i 的操作,将 i 交换到 j 的方案数为 \min(i,n-i+1,j,n-j+1),因此换到 j 和 n-j+1 的方案数是相等的,所以只要操作区间包含了 i,i 的期望位置即为 \frac{n+1}{2}。

则经过 m 次操作后 i 的不变的概率为 val=\frac{\frac{i \times (i-1)}{2}+\frac{(n-i)\times (n-i+1)}{2}}{\frac{n \times (n+1)}{2}},则 q_i=val \times i+(1-val)\times \frac{n+1}{2}。

时间复杂度 O(n \log m)。

Code

#include<bits/stdc++.h>
namespace fast_IO{
    #define IOSIZE 100000
        char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
    #define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
    #define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
    #define isdigit(ch) (ch>47&&ch<58)
    #define isspace(ch) (ch<33)
        template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
        template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
        template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
        inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
        inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
        inline void print(char x) { putchar(x); }
        inline void print(char *x) { while (*x) putchar(*x++); }
        inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
        inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
        inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
        inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
        inline void print(bool b) { putchar(b+48); }
        template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
        template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
        struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
        template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
        template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
    #define cout io
    #define cin io
}using namespace fast_IO;
using namespace std;
#define int long long
const int N=2e5+10,mod=998244353;
int n,m,p[N];
inline int qpow(int a,int b){
    int res=1;
    while(b){
      if(b&1)res=res*a%mod;
      a=a*a%mod;
      b>>=1;
    }
    return res;
}
inline int inv(int a){return qpow(a,mod-2);}
inline int add(int a,int b){return ((a+=b)>=mod)?(a-mod):a;}
inline int norm(int a,int b){return ((a-=b)<0)?(a+mod):a;}
signed main(){
    cin>>n>>m;
    for(int i=1;i<=n;++i)cin>>p[i];
    int ans=0,invsum=inv(n*(n+1)/2%mod);
    for(int i=1;i<=n;++i){
      ans=add(ans,i*i%mod);
      int val=qpow(add(i*(i-1)/2%mod,(n-i)*(n-i+1)/2%mod)*invsum%mod,m);
      ans=norm(ans,p[i]*add(val*i%mod,norm(1,val)*(n+1)%mod*((mod+1)>>1)%mod)%mod);
    }
    cout<<ans*qpow(n*(n+1)/2%mod,m)%mod<<"\n";
    return 0;
}