题解:P14352 排序

· · 题解

强烈谴责出题人不搬题目背景,速速加上。

场上多开了几个没用的数组导致怒挂这题所有分,亏麻了。

首先很显然 k>n 时答案就是 k=n 时的情况,这样我们就令 k\le n。

假如你会暴力的话那么你可以对 \forall n,k\le 10 打出以下的表:

1
2 2
4 6 6
8 18 24 24
16 54 96 120 120
32 162 384 600 720 720
64 486 1536 3000 4320 5040 5040
128 1458 6144 15000 25920 35280 40320 40320
256 4374 24576 75000 155520 246960 322560 362880 362880
512 13122 98304 375000 933120 1728720 2580480 3265920 3628800 3628800

简单观察可以发现三角形第 i 行右边为 i!,对于第 j 列的所有数,ans_{i,j}=ans_{i-1,j}\times (j+1),因此答案为 k!\times (k+1)^{n-k}。

:::info[场上 code]{open}

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

const int N=2e7+5,mod=998244353;
int n,k;

int ksm(int x,int y){
    int res=1;
    while(y){
        if(y&1) (res*=x)%=mod;
        (x*=x)%=mod;
        y>>=1;
    }
    return res;
}

signed main(){
    freopen("sorting.in","r",stdin);
    freopen("sorting.out","w",stdout);
    ios::sync_with_stdio(0);cin.tie(0);
    cin>>n>>k;k=min(n,k);
    int ans=1;
    for(int i=1;i<=k;i++) (ans*=i)%=mod;
    cout<<ans*ksm(k+1,n-k)%mod;
    return 0;
}

:::

证明:

:::info[Observation1] 注意到若 x 是前缀最大值,则他绝对会在当次操作中进行交换。且只会交换到下一个前缀最大值前。 :::

:::info[Observation2] 由 Observation1,一个数 a_i 被归位的下界是 \sum_{j=1}^i [a_j>a_i],因为一次操作只有一个 a_j>a_i 的 a_j 从 a_i 前交换到 a_i 后。 :::

:::info[Observation3] 注意到若 a_i\le i,则 a_i 每次只会被要归位的前缀 \max 向前被交换一位。

因此若 a_i\le i,需要保证 i-a_i\le k。

:::info[Observation4] 注意到设 b 为 a 的映射,即 b_{a_i}=i,则 b_i\le \max(i+k,n)。

一个 b 对应唯一的一个 a,因此拆掉 \max,分讨: