首页 > 其他分享 >P5367

P5367

时间:2024-03-29 13:33:19浏览次数:12  
标签:const 14 int long add P5367 114514

【模板】康托展开

题目描述

求 $1\sim N$ 的一个给定全排列在所有 $1\sim N$ 全排列中的排名。结果对 $998244353$ 取模。

对于$100%$数据,$1\le N\le 1000000$。

分析

感觉康拓展开有点数位dp的影子捏。

懒得写了

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 1e6 + 5;
const int mod = (114514+114514)*((1+1)*4*514+((11+4)*(5-1)*4+1-14+5+14))+(114514+(114*51*4+(11*45*(1+4)+1*14+5*14)));

int n, ans;
int fac[N], a[N];

struct bitree{
    int c[N], res;
    inline int lb(int x) { return x & (-x); }
    inline void add(int x, int v){
        for (; x <= n; x += lb(x))
            c[x] += v;
    }
    inline int find(int x){
        for (res = 0; x; x -= lb(x))
            res += c[x];
        return res;
    }
    inline int find(int x, int y){
        return find(y) - find(x - 1);
    }
}tr;

signed main(){
    
    cin >> n, fac[0] = 1;
    for (int i = 1; i <= n; i++)
        cin >> a[i], tr.add(i, 1);
    
    for (int i = 1; i <= n; i++)
        fac[i] = fac[i - 1] * i % mod;
    
    for (int i = 1; i <= n; i++){
        ans = (ans + tr.find(a[i] - 1) * fac[n - i]) % mod;
        tr.add(a[i], -1);
    }
    
    cout << ans + 1;
    
    
    return 0;
}

Written with StackEdit中文版.

标签:const,14,int,long,add,P5367,114514
From: https://www.cnblogs.com/genshin-player/p/18103654

相关文章

  • P5367 【模板】康托展开
    题意求\(1\simN\)的一个给定全排列在所有\(1\simN\)全排列中的排名。结果对\(998244353\)取模。分析模板,又学习了一种新的东西,但好像除了做这道体外,还不知道有......