跳到正文

个人记录

QOJ 14414

CR3mPAG约 8 分钟
原文
文章信息与版本

文章信息

发布
2026年7月29日
收录
2026年8月1日
更新
2026年11月19日
篇幅
约 8 分钟

版本

  1. v1当前

原题

题面

我们有一个长度为 nn 的整数序列 aa。nice⁡\operatorname{nice} 子序列 定义为:子序列中任意相邻两个元素不互质(即 gcd⁡>1\gcd>1)。求最长 nice⁡\operatorname{nice} 子序列的长度 ℓ\ell,以及长度为 ℓ\ell 的 nice⁡\operatorname{nice} 子序列个数(模 998244353998244353)。

数据范围:n≤2×105n \le 2\times 10^5,ai≤106a_i \le 10^6。

思路

依旧容斥依旧抄题解依旧 deepseek

尝试 dpdp 来刻画这个寻找子序列的过程。设 dpidp_i 表示以 aia_i 结尾的最长 nice⁡\operatorname{nice} 子序列长度,cnticnt_i 表示该长度方案数。每次找到一个 j<ij<i,使得 aia_i 与 aja_j 不互质且 dpjdp_j 最大。若最大值相同,则将方案数累加。时间复杂度 O(n2)O(n^2)。


不互质即元素间至少共享一个质因子。因此把“能与哪些前驱连接”转化为“通过哪些质因子链接”。所以我们只需要维护每个质因子的当前最优信息。设 bestpbest_p 表示:已处理过的元素中,以包含质因子 pp 结尾的最长 nice⁡\operatorname{nice} 子序列的长度。

当我们处理 x=aix=a_i 时,设 tt 是 xx 的不同质因子集合。要想把 xx 接在某个前驱后面,只需要前驱包含 tt 中的至少一个质因子。因此能接上的最长长度为:

L=max⁡p∈tbestpL=\max_{p \in t} best_p

于是以 xx 结尾的最长长度就是 dp_len⁡=L+1\operatorname{dp\_len}=L+1(如果 L=0L=0 说明没有可以连接的前驱,新开一个子序列)。


现在的问题成了:求出长度为 LL 且结尾元素与 xx 有公共质因子的方案总数。设 l⁡L\operatorname{l}_L 保存了所有长度为 LL 的方案的结尾信息。具体地,对于每个长度为 LL 的方案的结尾元素,我们记录它的质因子集合。

查询时,需要计算结尾元素质因子集合与 tt 有交的方案总数。

这是一个典型的“集合并集大小”问题,容斥原理得:

cnt⁡=∑∅≠s⊆t(−1)∣s∣+1⋅fs\operatorname{cnt}=\sum_{\emptyset \neq s \subseteq t}(-1)^{|s|+1} \cdot f_s

其中 fsf_s 表示长度为 LL 且结尾元素的质因子集合包含 ss 中所有质因子的方案数。为实现查询,在维护每一层 l⁡L\operatorname{l}_L 时,对于每个结尾元素,我们将其方案数加到它的所有非空质因子子集对应的“子集和”上。也就是说,用 l⁡L,prod\operatorname{l}_{L,prod} 存储:长度为 LL 且结尾元素的质因子集合包含当前子集(乘积为 prodprod))的总方案数。

查询时,枚举 tt 的所有非空子集,取出对应的累加值,乘上容斥系数求和即可。

计算得到当前 xx 的 dp_len⁡\operatorname{dp\_len} 和 cntcnt 后,将 cntcnt 加到 l⁡dp_len⁡\operatorname{l}_{\operatorname{dp\_len} } 中所有 tt 的非空子集对应的乘积上。

最后用 dp_len⁡\operatorname{dp\_len} 更新全局最大值和总方案数,并更新 bestp←max⁡(bestp,dp_len)best_p \leftarrow \max (best_p, \mathrm{dp\_len})。


复杂度分析

  • 质因数分解:线性筛预处理 1∼1061\sim 10^6 的最小质因子,单次分解 O(质因子个数)O(质因子个数)。
  • 每个数 xx 的不同质因子个数 ≤7≤ 7(因为 2×3×5×7×11×13×17>1062\times3\times5\times7\times11\times13\times17 > 10^6,子集个数 ≤128≤ 128。
  • 平均质因子个数约 2 32~3,子集个数平均很小。
  • 总操作数约为 O(n⋅2kavg)O(n \cdot 2^{k_{\text{avg}}}),在 106∼10710^6 \sim 10^7 级别,完全可行。
  • 由于每一层需要随机访问,可以使用 vector<unordered_map<int,int>> 存储每一层的子集累加值。
code
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e6+5;
const int mod=998244353;
int p[N];
vector<int> prime;
void init()
{
    for(int i=2;i<N;i++)
    {
        if(p[i]==0)
        {
            p[i]=i;
            prime.push_back(i);
        }
        for(int j:prime)
        {
            if(j>p[i]||i*j>N)
                break;
            p[i*j]=j;
        }
    }
}
vector<int> get(int x)
{
    vector<int> ans;
    while(x>1)
    {
        int tt=p[x];
        ans.push_back(tt);
        while(x%tt==0)
            x/=tt;
    }
    return ans;
}
int n,a[N],b[N],mx,tot;
unordered_map<int,int> l[N];
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    init();
    cin>>n;
    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
    {
        int x=a[i];
        if(x==1)
        {
            if(mx<1)
                mx=tot=1;
            else if(mx==1)
                tot=(tot+1)%mod;
            continue;
        }
        vector<int> t=get(x);
        int k=t.size();
        int mxlen=0;
        for(int j:t)
            mxlen=max(mxlen,b[j]);
        int tt=mxlen+1,cnt=0;
        if(mxlen==0)
            cnt=1;
        else
        {
            int m=mxlen;
            for(int mask=1;mask<(1<<k);mask++)
            {
                int prod=1,bits=0;
                for(int j=0;j<k;j++)
                {
                    if(mask&(1<<j))
                    {
                        prod*=t[j];
                        bits++;
                    }
                }
                auto it=l[m].find(prod);
                int val=(it!=l[m].end())?it->second:0;
                if(bits&1)
                    cnt=(cnt+val)%mod;
                else
                    cnt=(cnt-val+mod)%mod;
            }
        }
        if(tt>mx)
            mx=tt,tot=cnt;
        else if(tt==mx)
            tot=(tot+cnt)%mod;
        for(int mask=1;mask<(1<<k);mask++)
        {
            int prod=1;
            for(int j=0;j<k;j++)
            {
                if(mask&(1<<j))
                    prod*=t[j];
            }
            l[tt][prod]=(l[tt][prod]+cnt)%mod;
        }
        for(int j:t)
            b[j]=max(b[j],tt);
    }
    cout<<mx<<"\n"<<tot;
    return 0;
}
原文