Featured image of post 洛谷P5367题解

洛谷P5367题解

封面来源

Pixiv carbonated chiwawa-68295064

康托展开可以用来求哈希

公式

$$ X = \sum^n_{i-1}A[i]\times(n-i)!+1 $$

然后还要用树状数组优化

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 998244353;
const int N = 1000010;
int fac[N]={1,1},a[N],c[N];
int n;
inline int lowbit(int x){
    return x&(-x);
}
inline void modify(int p){
    while(p<=n){
        ++c[p];
        p+=lowbit(p);
    }
    return;
}
inline int ask(int p){
    int s =0 ;
    while(p){
        s+=c[p];
        p-=lowbit(p);
    }
    return s;
}
signed main(){
    int s=0;
    cin>>n;
    for(int i=1;i<=n;i++){
        cin>>a[i];
    }
    for(int i=2;i<=n;i++){
        fac[i]=(fac[i-1]*i)%mod;
    }
    for(int i=1;i<=n;i++){
        (s+=(fac[n-i])*(a[i]-1-ask(a[i]-1))%mod)%=mod;
        modify(a[i]);
    }
    cout<<s+1<<endl;
}
所念皆星河
Built with Hugo
主题 StackJimmy 设计