Description
黑妹又来玩游戏了,这次她面对的是两个长度为 n n n的正整数序列 A A A和 B B B,并且她要维护两个二元序对集合 S 1 S_1 S1和 S 2 S_2 S2,初始时这两个集合是空的,每一次她可以进行一次操作,选择满足以下条件的两个序对 ( i , j ) (i, j) (i,j)和 ( p , q ) (p, q) (p,q),
1. ( i , j ) (i, j) (i,j)不在 S 1 S_1 S1中并且 ( p , q ) (p, q) (p,q)不在 S 2 S_2 S2中。
2. A i < B j , A q > B p A_i<B_j, A_q > B_p Ai<Bj,Aq>Bp。
3.$A_i 和 和 和B_j $不互质, $A_q $和 B p B_p Bp 不互质。
4. g c d ( A i , B j ) gcd(A_i, B_j) gcd(Ai,Bj) 和 g c d ( A q , B p ) gcd(A_q, B_p) gcd(Aq,Bp) 不互质。
然后把 ( i , j ) (i, j) (i,j)加入集合 S 1 S_1 S1, ( p , q ) (p, q) (p,q)加入集合 S 2 S_2 S2。
但是她又很快玩腻了这个游戏,她现在想知道她最多能进行多少次操作。
Input
第一行一个整数 T T T表示测试数据组数。
对于每组数据:
第一行一个整数 n n n表示序列的长度。
接下来一行 n n n个整数表示序列 A A A的每个元素。
接下来一行 n n n个整数表示序列 B B B的每个元素。
( 1 ≤ T ≤ 10 , 1 ≤ n ≤ 400 , 1 ≤ A i , B i ≤ 1 0 9 ) (1\le T\le 10,1\le n\le 400,1\le A_i,B_i\le 10^9) (1≤T≤10,1≤n≤400,1≤Ai,Bi≤109)
Output
对于每组数据输出一行表示答案。
Sample Input
2
4
5 2 3 4
5 2 3 4
4
2 5 6 14
3 4 7 10
Sample Output
1
3
Solution
首先预处理出所有二元组 ( i , j ) (i,j) (i,j)和 ( p , q ) (p,q) (p,q),源点向每个 ( i , j ) (i,j) (i,j)建容量为 1 1 1的边,每个 ( p , q ) (p,q) (p,q)向汇点建容量为 1 1 1的边,对于可以配对的二元组之间建容量为 1 1 1的边,那么问题变为求该网络的最大流,但是这样边数巨大,注意到 ( i , j ) (i,j) (i,j)和 ( p , q ) (p,q) (p,q)可以连边当且仅当其存在大于 1 1 1的公因子,故再建一排点表示素因子, ( i , j ) (i,j) (i,j)向 g c d ( A i , B j ) gcd(A_i,B_j) gcd(Ai,Bj)的所有素因子建容量为 1 1 1的边, g c d ( A q , B p ) gcd(A_q,B_p) gcd(Aq,Bp)的所有素因子向 ( p , q ) (p,q) (p,q)连容量为 1 1 1的边,这样可以大规模降低边数,进而求最大流即得答案
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define maxn 800005
#define maxm 6000005
#define INF 0x3f3f3f3f
int pp[400005],res,mark[400005];
void pre(int n=400000)
{
res=0;
for(int i=2;i<=n;i++)
{
if(!mark[i])pp[++res]=i;
for(int j=2*i;j<=n;j+=i)mark[j]=1;
}
}
int head[maxn],cur[maxn],d[maxn],st[maxm],s,e,no;//s为源点,e为汇点,n为点数,no为边数
struct point
{
int u,v,flow,next;
point(){};
point(int x,int y,int z,int w):u(x),v(y),next(z),flow(w){};
}p[maxm];
void add(int x,int y,int z)//从x到y建容量为z的边
{
p[no]=point(x,y,head[x],z);//前向弧,标号为偶
head[x]=no++;
p[no]=point(y,x,head[y],0);//后向弧,标号为奇
head[y]=no++;
}
void init()//初始化
{
memset(head,-1,sizeof(head));
no=0;
}
bool bfs()
{
int i,x,y;
queue<int>q;
memset(d,-1,sizeof(d));
d[s]=0;
q.push(s);
while(!q.empty())
{
x=q.front();
q.pop();
for(i=head[x];i!=-1;i=p[i].next)
{
if(p[i].flow&& d[y=p[i].v]<0)
{
d[y]=d[x]+1;
if(y==e)
return true;
q.push(y);
}
}
}
return false;
}
int dinic()//最大流
{
int i,loc,top,x=s,nowflow,maxflow=0;
while(bfs())
{
memcpy(cur,head,sizeof(head));
top=0;
while(true)
{
if(x==e)
{
nowflow=INF;
for(i=0;i<top;i++)
{
if(nowflow>p[st[i]].flow)
{
nowflow=p[st[i]].flow;
loc=i;
}
}
for(i=0;i<top;i++)
{
p[st[i]].flow-=nowflow;
p[st[i]^1].flow+=nowflow;
}
maxflow+=nowflow;
top=loc;
x=p[st[top]].u;
}
for(i=cur[x];i!=-1;i=p[i].next)
if(p[i].flow&&d[p[i].v]==d[x]+1)
break;
cur[x]=i;
if(i!=-1)
{
st[top++]=i;
x=p[i].v;
}
else
{
if(!top)
break;
d[x]=-1;
x=p[st[--top]].u;
}
}
}
return maxflow;
}
int T,n,a[405],b[405];
map<int,int>M1,M2;
map<int,int>::iterator it;
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int main()
{
pre();
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)scanf("%d",&b[i]);
M1.clear(),M2.clear(),init();
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(a[i]!=b[j])
{
int g=gcd(a[i],b[j]);
if(a[i]<b[j])M1[g]++;
else M2[g]++;
}
int n1=M1.size(),n2=M2.size();
int id=1;
for(it=M1.begin();it!=M1.end();it++,id++)
{
int x=it->first,num=it->second;
for(int i=1;i<=res&&pp[i]<=x;i++)
if(x%pp[i]==0)
{
add(id,n1+n2+i,num);
while(x%pp[i]==0)x/=pp[i];
}
if(x>1)
{
pp[++res]=x;
add(id,n1+n2+res,num);
}
}
id=n1+1;
for(it=M2.begin();it!=M2.end();it++,id++)
{
int x=it->first,num=it->second;
for(int i=1;i<=res&&pp[i]<=x;i++)
if(x%pp[i]==0)
{
add(n1+n2+i,id,num);
while(x%pp[i]==0)x/=pp[i];
}
if(x>1)
{
pp[++res]=x;
add(n1+n2+res,id,num);
}
}
s=0,e=n1+n2+res+1;
id=1;
for(it=M1.begin();it!=M1.end();it++,id++)add(s,id,it->second);
id=n1+1;
for(it=M2.begin();it!=M2.end();it++,id++)add(id,e,it->second);
printf("%d\n",dinic());
}
return 0;
}