[CF1656H] Equal LCM Subsets
Disjoint_cat · · 题解
Statement
给定集合
Data range:
Sol
怎么题解区
你考虑值域是 map 之类的维护每种质因数出现次数最大的是哪些数。
如果一个质因数出现最多的数全在
但是现在值域超级无敌大,就算用 Pollard-Rho 也很难分解出来。
但是可以发现,其实我们不需要真的把质因数全都分解出来,只需要分解到「和质因数一样有足够好的性质」即可。
具体地,我们尝试确定一个「伪质数集」
-
- 对
A,B 中的每一个元素,都可以唯一分解为若干个P 中元素(「伪质因数」)的乘积。
(取
此时,一个集合的
之所以质因数分解低效,是因为可能有用的质数太多,所以我们要试图让
考虑初始时令
假设现要插入一个元素
- 显然,应先尝试分解
x 。所以对每个y\in P ,不断令x\gets \frac xy 直到无法整除; - 如果
x=1 则当然无需操作;如果x 已经与P 中所有数互质,直接将x 加入集合P 即可; - 否则设存在
y\in P ,\gcd(x,y)=g>1 。此时,从P 中删除y ,然后递归插入g,\frac xg,\frac yg 即可。
这样做,插入后的
考虑直接实现上述算法的时间复杂度。势能分析,令势能为
当然这个界很松跑不满,而且实际上递归时会有很多求
显然
总时间复杂度 可能是因为数据没有对着这个做法造?
Code
// Psuedo-prime factorization is so cute meow!
#include<bits/stdc++.h>
using namespace std;
#ifdef DEBUG
#include "templates/debug.h"
#else
#define dbg(...) (void)0
#define here (void)0
#endif
using ll=long long;
using VI=vector<int>;
using PII=pair<int,int>;
#define endl '\n'
#define PB emplace_back
#define PPB pop_back
#define MP make_pair
#define ALL(Name) Name.begin(),Name.end()
#define GI greater<int>
#define fi first
#define se second
using LL=__int128;
void RD(LL &x)
{
x=0;
static char ch=' ';
while(!isdigit(ch=cin.get()));
while(isdigit(ch))x=x*10+ch-'0',ch=cin.get();
}
void PT(LL x)
{
static char ch[40]; static int tp=0;
while(x)ch[tp++]=x%10+'0',x/=10;
while(tp)cout<<ch[--tp];
}
const int N=1005;
int n,m;
LL a[N],b[N];
vector<LL> F;
vector<map<int,pair<VI,VI>>> C;
vector<PII> fa[N],fb[N];
bool ra[N],rb[N];
void ins(LL x,int st=0)
{
for(int i=st;i<F.size();i++)
while(!(x%F[i]))x/=F[i];
if(x==1)return;
for(int i=st;i<F.size();i++)
{
LL g=__gcd(x,F[i]);
if(g>1)
{
LL t=F[i]/g;
F.erase(F.begin()+i);
F.PB(g),ins(t,F.size()-1),ins(x,i);
return;
}
}
F.PB(x);
}
void fac(LL x,vector<PII> &f)
{
f.clear();
for(int i=0;i<F.size();i++)
{
int c=0;
while(!(x%F[i]))x/=F[i],c++;
if(c)f.PB(i,c);
}
assert(x==1);
}
int main()
{
ios::sync_with_stdio(false),cin.tie(nullptr);
int _;cin>>_;while(_--)
{
cin>>n>>m;
F.clear();
for(int i=1;i<=n;i++)RD(a[i]),ins(a[i]);
for(int i=1;i<=m;i++)RD(b[i]),ins(b[i]);
C.clear(),C.resize(F.size());
for(int i=1;i<=n;i++)
{
fac(a[i],fa[i]),ra[i]=1;
for(auto [p,v]:fa[i])C[p][v].fi.PB(i);
}
for(int i=1;i<=m;i++)
{
fac(b[i],fb[i]),rb[i]=1;
for(auto [p,v]:fb[i])C[p][v].se.PB(i);
}
queue<int> da,db;
auto del=[&](auto &qwq)
{
while(!qwq.empty())
{
auto &[v,p]=*qwq.rbegin();
if(p.fi.empty()||p.se.empty())
{
for(int i:p.se)db.push(i);
for(int i:p.fi)da.push(i);
qwq.erase(prev(qwq.end()));
}
else break;
}
};
for(auto &i:C)del(i);
while(1)
if(!da.empty())
{
int x=da.front();da.pop();
if(!ra[x])continue;
ra[x]=0;
for(auto [f,c]:fa[x])
if(C[f].count(c))
C[f][c].fi.erase(lower_bound(ALL(C[f][c].fi),x)),del(C[f]);
}
else if(!db.empty())
{
int x=db.front();db.pop();
if(!rb[x])continue;
rb[x]=0;
for(auto [f,c]:fb[x])
if(C[f].count(c))
C[f][c].se.erase(lower_bound(ALL(C[f][c].se),x)),del(C[f]);
}
else break;
VI sa,sb;
for(int i=1;i<=n;i++)if(ra[i])sa.PB(i);
for(int i=1;i<=m;i++)if(rb[i])sb.PB(i);
if(sa.empty()||sb.empty())cout<<"NO\n";
else
{
cout<<"YES\n"<<sa.size()<<" "<<sb.size()<<endl;
for(int i:sa)PT(a[i]),cout<<" "; cout<<endl;
for(int i:sb)PT(b[i]),cout<<" "; cout<<endl;
}
}
return 0;
}