QOJ 14414

· · 个人记录

原题

题面

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

数据范围:n \le 2\times 10^5,a_i \le 10^6。

思路

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

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

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

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

L=\max_{p \in t} best_p

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

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

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

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

\operatorname{cnt}=\sum_{\emptyset \neq s \subseteq t}(-1)^{|s|+1} \cdot f_s

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

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

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

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

复杂度分析

::::success[code]

#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;
}