NOIP 模拟 $25\; \rm random$
题解 \(by\;zj\varphi\)
期望好题。
通过推规律可以发现每个逆序对的贡献都是 \(1\),那么在所有排列中有多少逆序对,贡献就是多少。
\[\rm num_i=(i-1)!\sum_{j=1}^{i-1}j+i*num_{i-1}\\
\]
最后化减完可以得到
\[\rm ans=\frac{\sum_{i=1}^n\frac{i*(i-1)}{3}}{n}\\
ans=\frac{n^2-1}{9}
\]
Code:
include<bits/stdc++.h>
#define ri register signed
#define p(i) ++i
using namespace std;
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++;
template<typename T>inline void read(T &x) {
ri f=1;x=0;register char ch=gc();
while(!isdigit(ch)) {if (ch=='-') f=0;ch=gc();};
while(isdigit(ch)) {x=(x<<1)+(x<<3)+(ch^48);ch=gc();}
x=f?x:-x;
}
char OPUT[100];
template<typename T>inline void print(T x){
if(x<0) putchar('-'),x=-x;
if(!x) return putchar('0'),(void)putchar('\n');
ri cnt=0;
while(x) OPUT[++cnt]=x%10,x/=10;
for (ri i(cnt);i;--i) putchar(OPUT[i]+'0');
return (void)putchar('\n');
}
}
using IO::read;using IO::print;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
typedef long long ll;
static const int MOD=998244353;
int T,inv;
ll n;
inline int fpow(int x,int y) {
int res=1;
while(y) {
if (y&1) res=1ll*res*x%MOD;
x=1ll*x*x%MOD;
y>>=1;
}
return res;
}
inline int main() {
//FI=freopen("nanfeng.in","r",stdin);
//FO=freopen("nanfeng.out","w",stdout);
read(T);
inv=fpow(9,MOD-2);
for (ri z(1);z<=T;p(z)) {
read(n);
n%=MOD;
print((n*n%MOD-1ll)*inv%MOD);
}
return 0;
}
}
int main() {return nanfeng::main();}