【3】题解:P9004 [RC-07] Abnormal Permutation Tuples【动态规划 DP】【前缀和优化】【卡常】

· · 题解

观测到 \mathcal O(n^8m^2) 时间复杂度,来看看会不会。

题目:m 个有序长为 n 的排列,每个排列两个权值作为比较权,逆序对和带权逆序对(字典序)。求这样的排列的满足字典序单调增,逆序对单调减的个数。

写题解前先膜 Shellchen。

::::info[打出来的表]{open}

字被洛谷压缩了,拖动到浏览器中查看。

那么此时就能注意到长度为 n 的排列逆序对数可以被分为 n 个长度为 (n-1)! 的循环,每个循环处第一个都与 (n-1)! 完全一致,上下两组为 +1 的关系。当然这个说明非常不优美,下面给出证明。

::::info[结论证明]{open} 第一组排列的本质就是删去在最开头的 1,因为其无法造成任何贡献。那么就变成了 (n-1)! 的问题,也就是为什么其和上一组排列完全一样。

考虑第 2\sim n 组(设为 x)的构造方式。你发现,每隔 (n-1)! 轮,发生的改变是:

这个怎么证明呢?每一组最开头的元素就是 x 在最开头,其他按照顺序排列,本质就是把 x 删除了,其他进行一个 (n-1)! 的排列。

为什么这样会导致逆序对数增加恰好 1?考虑交换两个相邻元素(a 和 b,其中 a 在 b 左侧)对逆序对数的贡献。

那么,每次我们交换都只能交换 a<b 的情况。否则结论不成立。这个怎么证明呢?每次 a 都属于开头的情况,且 b=a+1,则 a 必定在 b 左侧,且 a<b 恒成立。每次交换,逆序对恒定大 1。

结论成立。

完成简单的证明后,我们开始按照这个设计 DP。直接预处理排列逆序对数是显然不可行的。直接按照状态设计,基本省略存储排列信息,只由逆序对的大小来存储,那么本质就是在这个 n! 的数列中选出一个合法递减序列。问题本身是简单的。但是序列非常长,那么我们就考虑如何优化这个问题。对于设计状态,n 个连续段中,每一连续段是单调增的。我们只关心是否单调减,那么只需要在状态中存储目前排列集的数值最大最小值,合并区间即可。每次,分两类情况转移:

  1. 切换更多的段,在段内选择一些递减元素,这部分状态可以直接用 f_{i-1} 得出。
  2. 直接以这个段为开头。
  3. 不选择一个段,直接跳过(正经 LIS 没有这个过程,但是这是 LIS 计数,所以要带上)。

是一个 LIS 的思路。那么根据这个大致思考,可以列出设计:f_{i,\text{cnt},\text{sta},\text{end},\text{blk}} 为四个所需状态(i 为排列长度,以辅助转移不同阶的情况),\text{cnt} 为选择的排列数,\text{sta} 为逆序对最大值(开头处),\text{end} 为逆序对最小值(结尾处),\text{blk} 为目前考虑的排列所在的段(与 i 强相关)。

首先考虑第一类转移。枚举新状态所需:{\color{blue}\text{max}} 表段内逆序对最大值,{\color{blue}\text{min}} 表段内逆序对最小值,{\color{blue}\text{new}} 拼接的新段个数。有转移:

f_{i,\text{cnt}+{\color{blue}\text{new}},\text{sta},{\color{blue}\text{min}},\text{blk}}\gets f_{i,\text{cnt},\text{sta},\text{end},\text{blk}-1}\times f_{i-1,{\color{blue}\text{new}},{\color{blue}\text{max}}-\text{blk}+1,{\color{blue}\text{min}}-\text{blk}+1,i-1}[\text{end}>{\color{blue}\text{max}}]

状态意义:考虑直接在选择的合法区间内找到一个子区间来转移。具体的,我们在 i 阶层,已经选好了 \text{blk}-1 个当前层的区间内的数,得出了 [\text{sta},\text{end}] 的取值范围,则目标是在当前第 i 阶第 \text{blk} 个段找到一组数,值域 [1,\text{end}) 的最长下降子序列(即 [\text{end}>{\color{blue}\text{max}}])。注意首尾一定是序列的最小 / 最大值,所以直接这么写就能获得到答案。

然后非常简单的是第二类。此时偏移量是一样的,有转移(此时保证 \text{cnt}=0):

f_{i,\text{cnt}+{\color{blue}\text{new}},{\color{blue}\text{max}},{\color{blue}\text{min}},\text{blk}}\gets f_{i-1,{\color{blue}\text{new}},{\color{blue}\text{max}}-\text{blk}+1,{\color{blue}\text{min}}-\text{blk}+1,i-1}

这个东西和上面那个是一样的。

对于第三类,有 f_{i,\text{cnt},\text{sta},\text{end},\text{blk}}\gets f_{i,\text{cnt},\text{sta},\text{end},\text{blk}-1},意义就是直接不选择,但是要注意转移代价。

考虑完三类后就可以直接转移了。然后就能得出一个 \mathcal O(n^{10}m^2) 的 DP,优化为 \mathcal O(n^{8}m^2) 就能通过了。

这题给我们的启示是:当常规 DP 模型无法直接解决问题时,可以试图通过元素性质转化为一个简单的 DP,再通过大小变动性质使得其能用一个更优的方法表示出来,再转移。属于状态设计技巧。

::::info[直接暴力 DP] 这里第二种转移为了方便改成了刷表。

namespace brute_dp{
    ll f[N][M][V][V][N];
    il void sol(){
        f[1][1][0][0][1]=1;
        for(int i=2;i<=n;++i){
            int max_rev=i*(i-1)/2, pre_max=(i-1)*(i-2)/2;
            for(int cnt=0;cnt<=m;++cnt) for(int blk=1;blk<=i;++blk){
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed)
                    (f[i][cnt][st][ed][blk]+=f[i][cnt][st][ed][blk-1])%=P;
                if(!cnt) 
                    for(int st=0;st<=pre_max;++st) for(int ed=0;ed<=st;++ed)
                        for(int nw=0;nw+cnt<=m;++nw)
                            (f[i][cnt+nw][st+blk-1][ed+blk-1][blk]+=f[i-1][nw][st][ed][i-1])%=P;
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed){
                    for(int mx=ed-1;mx-blk+1>=0;--mx) for(int mn=mx;mn-blk+1>=0;--mn) 
                        for(int nw=0;nw+cnt<=m;++nw)
                            (f[i][cnt+nw][st][mn][blk]+=f[i][cnt][st][ed][blk-1]*f[i-1][nw][mx-blk+1][mn-blk+1][i-1]%P)%=P;
                }
            }
        }
        for(int i=1;i<=n;++i){
            int max_rev=i*(i-1)/2;
            for(int cnt=1;cnt<=m;++cnt){
                ll ans=0;
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed) (ans+=f[i][cnt][st][ed][i])%=P;
                cout<<ans<<" ";
            }
            cout<<"\n";
        }
        return;
    }
}

::::

::::info[先优化掉 \text{blk} 维]

namespace faster_dp{
    ll f[N][M][V][V][2]; // blk: [0: pre, 1: now]
    il void sol(){
        f[1][1][0][0][0]=1;
        for(int i=2;i<=n;++i) for(int blk=1;blk<=i;++blk){
            int max_rev=i*(i-1)/2, pre_max=(i-1)*(i-2)/2;
            for(int cnt=0;cnt<=m;++cnt)
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed)
                    f[i][cnt][st][ed][1]=0;
            for(int cnt=0;cnt<=m;++cnt){
                if(!cnt) 
                    for(int st=0;st<=pre_max;++st) for(int ed=0;ed<=st;++ed)
                        for(int nw=0;nw+cnt<=m;++nw)
                            (f[i][cnt+nw][st+blk-1][ed+blk-1][1]+=f[i-1][nw][st][ed][0])%=P;
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed){
                    for(int mx=ed-1;mx-blk+1>=0;--mx) for(int mn=mx;mn-blk+1>=0;--mn) 
                        for(int nw=0;nw+cnt<=m;++nw)
                            (f[i][cnt+nw][st][mn][1]+=f[i][cnt][st][ed][0]*f[i-1][nw][mx-blk+1][mn-blk+1][0]%P)%=P;
                }
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed)
                    (f[i][cnt][st][ed][0]+=f[i][cnt][st][ed][1])%=P;
            }
        }
        for(int i=1;i<=n;++i){
            int max_rev=i*(i-1)/2;
            for(int cnt=1;cnt<=m;++cnt){
                ll ans=0;
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed) (ans+=f[i][cnt][st][ed][0])%=P;
                cout<<ans<<" ";
            }
            cout<<"\n";
        }
        return;
    }
}

::::

::::info[更利于前缀和优化的转移顺序]

好像忘记把 {\color{blue}\text{new}} 维提到前面了,算了这不重要 qwq。

namespace faster_dp{
    ll f[N][M][V][V][2]; // blk: [0: pre, 1: now]
    il void sol(){
        f[1][1][0][0][0]=1;
        for(int i=2;i<=n;++i) for(int blk=1;blk<=i;++blk){
            int max_rev=i*(i-1)/2, pre_max=(i-1)*(i-2)/2;
            for(int cnt=0;cnt<=m;++cnt)
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed)
                    f[i][cnt][st][ed][1]=0;
            for(int cnt=0;cnt<=m;++cnt){
                if(!cnt) 
                    for(int st=0;st<=pre_max;++st) for(int ed=0;ed<=st;++ed)
                        for(int nw=0;nw+cnt<=m;++nw)
                            (f[i][cnt+nw][st+blk-1][ed+blk-1][1]+=f[i-1][nw][st][ed][0])%=P;
                for(int st=0;st<=max_rev;++st){
                    for(int mx=blk-1;mx<=st-1;++mx) for(int mn=blk-1;mn<=mx;++mn)  
                         for(int ed=mx+1;ed<=st;++ed)
                            for(int nw=0;nw+cnt<=m;++nw)
                                (f[i][cnt+nw][st][mn][1]+=f[i][cnt][st][ed][0]*f[i-1][nw][mx-blk+1][mn-blk+1][0]%P)%=P;
                }
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed)
                    (f[i][cnt][st][ed][0]+=f[i][cnt][st][ed][1])%=P;
            }
        }
        for(int i=1;i<=n;++i){
            int max_rev=i*(i-1)/2;
            for(int cnt=1;cnt<=m;++cnt){
                ll ans=0;
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed) (ans+=f[i][cnt][st][ed][0])%=P;
                cout<<ans<<" ";
            }
            cout<<"\n";
        }
        return;
    }
}

::::

::::info[直接前缀和优化,但是 80 分的做法]

namespace faster_dp{
    ll f[N][M][V][V][2]; // blk: [0: pre, 1: now]
    ll s[M][V][V]; // sum of different st
    il void sol(){
        f[1][1][0][0][0]=1;
        for(int i=2;i<=n;++i) for(int blk=1;blk<=i;++blk){
            int max_rev=i*(i-1)/2, pre_max=(i-1)*(i-2)/2;
            for(int cnt=0;cnt<=m;++cnt)
                for(int st=0;st<=max_rev;++st){
                    s[cnt][st][0]=f[i][cnt][st][0][0];
                    for(int ed=0;ed<=st;++ed){
                        if(ed) s[cnt][st][ed]=(s[cnt][st][ed-1]+f[i][cnt][st][ed][0])%P;
                        f[i][cnt][st][ed][1]=0;
                    }
                }
            for(int cnt=0;cnt<=m;++cnt){
                if(!cnt) 
                    for(int st=0;st<=pre_max;++st) for(int ed=0;ed<=st;++ed)
                        for(int nw=0;nw+cnt<=m;++nw)
                            (f[i][cnt+nw][st+blk-1][ed+blk-1][1]+=f[i-1][nw][st][ed][0])%=P;
                for(int st=0;st<=max_rev;++st) for(int nw=0;nw+cnt<=m;++nw){
                    for(int mx=blk-1;mx<=st-1;++mx) for(int mn=blk-1;mn<=mx;++mn){
                        (f[i][cnt+nw][st][mn][1]+=(s[cnt][st][st]-s[cnt][st][mx]+P)%P*f[i-1][nw][mx-blk+1][mn-blk+1][0]%P)%=P;
                        /* for(int ed=mx+1;ed<=st;++ed)
                            (f[i][cnt+nw][st][mn][1]+=f[i][cnt][st][ed][0]*f[i-1][nw][mx-blk+1][mn-blk+1][0]%P)%=P; */
                    }
                }
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed)
                    (f[i][cnt][st][ed][0]+=f[i][cnt][st][ed][1])%=P;
            }
        }
        for(int i=1;i<=n;++i){
            int max_rev=i*(i-1)/2;
            for(int cnt=1;cnt<=m;++cnt){
                ll ans=0;
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed) (ans+=f[i][cnt][st][ed][0])%=P;
                cout<<ans<<" ";
            }
            cout<<"\n";
        }
        return;
    }
}

::::

极度卡常。需要调整维度顺序等。D 老师太强了。

::::success[通过代码,但是没啥可读性(7850ms)]

// p: Luogu P9004
// u: https://www.luogu.com.cn/problem/P9004
// m: 512 MB
// t: 5000 ms

// #define FileIO
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define fi first
#define se second
#define il inline
bool stMem; double stT=clock();
const int N=15+1, M=30+1, V=105+1;
ll P=998244353;
il ll qp(ll a,ll b=P-2){
    ll res=1;
    for(;b;b>>=1,a=(a*a)%P) if(b&1) res=(res*a)%P;
    return res;
}
// ---------------------------------------------------- //
int n,m;
il int gmn(int x,int y){ return x<y?x:y; }
namespace faster_dp{
    // * 进行了 cnt 维度后置优化,blk 维度前置优化
    ll f[N][V][V][M], g[N][V][V][M]; // blk: [f: pre, g: now]
    ll s[V][V][M]; // sum of different st
    il void sol(){
        f[1][0][0][1]=1;
        for(int i=2;i<=n;++i) for(int blk=1;blk<=i;++blk){
            memset(g[i],0,sizeof(g[i]));
            int max_rev=i*(i-1)/2, pre_max=(i-1)*(i-2)/2;
            for(int st=0;st<=max_rev;++st){
                for(int cnt=0;cnt<=m;++cnt) s[st][0][cnt]=f[i][st][0][cnt];
                for(int ed=1;ed<=st;++ed) for(int cnt=0;cnt<=m;++cnt)
                    s[st][ed][cnt]=(s[st][ed-1][cnt]+f[i][st][ed][cnt])%P;
            }
            for(int st=0;st<=pre_max;++st) for(int ed=0;ed<=st;++ed) for(int nw=0;nw<=m;++nw){
                ll pre_sum=f[i-1][st][ed][nw];
                if(pre_sum) g[i][st+blk-1][ed+blk-1][nw]+=pre_sum;
            }
            for(int mx=blk-1;mx<=gmn(max_rev-1,pre_max+blk-1);++mx){
                int mx_blk_1=mx-blk+1;
                for(int nw=1;nw<m;++nw) for(int mn=blk-1;mn<=mx;++mn){
                    ll dp=f[i-1][mx_blk_1][mn-blk+1][nw];
                    if(dp) for(int st=mx+1;st<=max_rev;++st) for(int cnt=1;cnt+nw<=m;++cnt){
                        ll su=(s[st][st][cnt]-s[st][mx][cnt]); if(su<0) su+=P;
                        if(su) (f[i][st][mn][cnt+nw]+=su*dp)%=P;
                    }
                }
            }
            for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed)
                for(int cnt=0;cnt<=m;++cnt)
                    (f[i][st][ed][cnt]+=g[i][st][ed][cnt])%=P;
        }
        for(int i=1;i<=n;++i){
            int max_rev=i*(i-1)/2;
            for(int cnt=1;cnt<=m;++cnt){
                ll ans=0;
                for(int st=0;st<=max_rev;++st) for(int ed=0;ed<=st;++ed) (ans+=f[i][st][ed][cnt])%=P;
                cout<<ans<<" ";
            }
            cout<<"\n";
        }
        return;
    }
}
// ---------------------------------------------------- //
il void solve(){
    cin>>n>>m>>P;
    faster_dp::sol();
    return;
}
// ---------------------------------------------------- //
bool edMem;
int main(){
    #ifdef FileIO
        freopen("file.in","r",stdin);
        freopen("file.out","w",stdout);
    #endif
    ios::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int T=1; //cin>>T;
    while(T--) solve();
    cerr<<"\n";
    cerr<<"[Info] End Of Running. \n";
    cerr<<"       Time Used   : "<<(clock()-stT)/CLOCKS_PER_SEC*1000.00<<" ms\n";
    cerr<<"       Memory Used : "<<(&stMem-&edMem)/1048576.00<<" Mib\n";
    return 0;
}

::::

Bonus:D 老师写的 avx 优化 4320ms。更加优化的算法有本质区别,最优解是打表的。我不管。

::::warning[AI 生成]

#pragma GCC target("avx512f,avx512bw,avx512vl")

#include <bits/stdc++.h>
#include <immintrin.h>
using namespace std;

#define ll long long
#define il inline

const int N = 15 + 1, M = 30 + 1, V = 105 + 1;
ll P = 998244353;

int n, m;
int Pi;
uint64_t PHI;

namespace faster_dp{
    int f[2][V][V][M], g[V][V][M];
    int s[V][V][M];

    il __m512i cond_sub(__m512i v, __m512i pi){
        __mmask16 mask = _mm512_cmp_epu32_mask(v, pi, _MM_CMPINT_NLT);
        return _mm512_mask_sub_epi32(v, mask, v, pi);
    }

    il void merge_and_build_s(int cur, int max_rev){
        __m512i vpi = _mm512_set1_epi32(Pi);
        for(int st = 0; st <= max_rev; ++st){
            {
                int *fp = f[cur][st][0], *gp = g[st][0];
                int cnt = 0;
                for(; cnt + 16 <= M; cnt += 16){
                    __m512i a = _mm512_loadu_si512((void*)(fp + cnt));
                    __m512i b = _mm512_loadu_si512((void*)(gp + cnt));
                    __m512i v = cond_sub(_mm512_add_epi32(a, b), vpi);
                    _mm512_storeu_si512((void*)(fp + cnt), v);
                    _mm512_storeu_si512((void*)(s[st][0] + cnt), v);
                }
                for(; cnt < M; ++cnt){
                    int v = fp[cnt] + gp[cnt];
                    if(v >= Pi) v -= Pi;
                    fp[cnt] = v;
                    s[st][0][cnt] = v;
                }
            }
            for(int ed = 1; ed <= st; ++ed){
                int *fp = f[cur][st][ed], *gp = g[st][ed];
                int *sp = s[st][ed], *spp = s[st][ed - 1];
                int cnt = 0;
                for(; cnt + 16 <= M; cnt += 16){
                    __m512i a = _mm512_loadu_si512((void*)(fp + cnt));
                    __m512i b = _mm512_loadu_si512((void*)(gp + cnt));
                    __m512i v = cond_sub(_mm512_add_epi32(a, b), vpi);
                    _mm512_storeu_si512((void*)(fp + cnt), v);
                    __m512i sv = cond_sub(
                        _mm512_add_epi32(_mm512_loadu_si512((void*)(spp + cnt)), v), vpi);
                    _mm512_storeu_si512((void*)(sp + cnt), sv);
                }
                for(; cnt < M; ++cnt){
                    int v = fp[cnt] + gp[cnt];
                    if(v >= Pi) v -= Pi;
                    fp[cnt] = v;
                    int sv = spp[cnt] + v;
                    if(sv >= Pi) sv -= Pi;
                    sp[cnt] = sv;
                }
            }
        }
    }

    il void add_new_chain(int prev, int blk, int pre_max){
        int off = blk - 1;
        for(int st = 0; st <= pre_max; ++st){
            for(int ed = 0; ed <= st; ++ed){
                int *dst = g[st + off][ed + off];
                int *src = f[prev][st][ed];
                int nw = 0;
                for(; nw + 16 <= M; nw += 16){
                    __m512i a = _mm512_loadu_si512((void*)(dst + nw));
                    __m512i b = _mm512_loadu_si512((void*)(src + nw));
                    _mm512_storeu_si512((void*)(dst + nw), _mm512_add_epi32(a, b));
                }
                for(; nw < M; ++nw) dst[nw] += src[nw];
            }
        }
    }

    inline int barrett_mul_mod(int a, int b){
        uint64_t x = (uint64_t)a * b;
        uint64_t q = (uint64_t)(((__uint128_t)x * PHI) >> 61);
        uint64_t r = x - q * (uint64_t)Pi;
        if(r >= (uint64_t)Pi) r -= (uint64_t)Pi;
        return (int)r;
    }

    il void sol(){
        Pi = (int)P;
        PHI = (uint64_t)(((__uint128_t)1 << 61) / Pi);

        f[0][0][0][1] = 1;

        for(int i = 1; i <= n; ++i){
            if(i >= 2){
                int cur = (i + 1) & 1;
                int prev = i & 1;
                int max_rev = i * (i - 1) / 2;
                int pre_max = (i - 1) * (i - 2) / 2;

                memset(f[cur], 0, sizeof(f[cur]));

                for(int blk = 1; blk <= i; ++blk){
                    for(int st = 0; st <= max_rev; ++st)
                        memset(g[st], 0, sizeof(int) * (st + 1) * M);

                    add_new_chain(prev, blk, pre_max);

                    if(blk > 1){
                        int mx_hi = min(max_rev - 1, pre_max + blk - 1);

                        for(int st = blk; st <= max_rev; ++st){
                            int *ssp = s[st][st];
                            int mx_end = min(st - 1, mx_hi);

                            for(int mn = blk - 1; mn <= mx_end; ++mn){
                                long long tmp[M];
                                memset(tmp, 0, sizeof(tmp));
                                int mnp = mn - blk + 1;

                                for(int mx = blk - 1; mx <= mx_end; ++mx){
                                    int mxp = mx - blk + 1;
                                    int *prev_nw = f[prev][mxp][mnp];
                                    int *smp = s[st][mx];

                                    for(int nw = 1; nw < m; ++nw){
                                        int dp = prev_nw[nw];
                                        if(!dp) continue;
                                        int end = m - nw;
                                        for(int cnt = 1; cnt <= end; ++cnt){
                                            int su = ssp[cnt] - smp[cnt];
                                            if(su < 0) su += Pi;
                                            if(!su) continue;
                                            tmp[cnt + nw] += barrett_mul_mod(su, dp);
                                        }
                                    }
                                }

                                int *fst = f[cur][st][mn];
                                for(int cnt = 1; cnt <= m; ++cnt){
                                    if(!tmp[cnt]) continue;
                                    int add = (int)(tmp[cnt] % Pi);
                                    int v = fst[cnt] + add;
                                    if(v >= Pi) v -= Pi;
                                    fst[cnt] = v;
                                }
                            }
                        }
                    }

                    merge_and_build_s(cur, max_rev);
                }
            }

            // 立即输出当前阶 i 的答案
            int arr = (i + 1) & 1;
            int max_rev = i * (i - 1) / 2;
            for(int cnt = 1; cnt <= m; ++cnt){
                long long ans = 0;
                for(int st = 0; st <= max_rev; ++st)
                    for(int ed = 0; ed <= st; ++ed)
                        ans += f[arr][st][ed][cnt];
                printf("%lld ", ans % Pi);
            }
            puts("");
        }
    }
}

il void solve(){
    cin >> n >> m >> P;
    faster_dp::sol();
}

int main(){
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    solve();
    return 0;
}

::::