Newcoder 130 D.黑妹的游戏IV(最大流-dinic)

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 &lt; B j , A q &gt; B p A_i&lt;B_j, A_q &gt; 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) (1T10,1n400,1Ai,Bi109)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值