PY个康托展开
卡 PY 的题,和 BIT 一样,不说了
内存还卡的挺紧的?
具体而言,刚好打完的 BIT 扒过来用用,然后打个阶乘就完事儿了
没什么好说的
Code
fac=[0 for i in range(1000003)]
mod=998244353
n=0;ans=1
a=[0 for i in range(1000003)]
f=[0 for i in range(500005)]
def lowbit(x):
return x&(-x)
def add(x, k):
while x<=n :
f[x]+=k
x+=lowbit(x)
def ask(x):
Res=0
while x>0:
Res+=f[x]
x-=lowbit(x)
return Res
if __name__=='__main__':
n=int(input())
s=input().split()
fac[0]=1
for i in range(1,n+1):
fac[i]=fac[i-1]*i%mod
a[i]=int(s[i-1])
add(i,1)
for i in range(1,n+1):
# print(ask(a[i]-1))
ans+=(ask(a[i]-1))*fac[n-i]%mod
add(a[i],-1)
print(ans)