设有i,j,满足a[i]|a[j]且a[j]/a[i]为质数,那么显然可以在i和j之间连一条费用为c[i]*c[j]流量为inf的边。然后所有点向S或T连费用为0流量b[i]的边。
注意满足上述条件的(i,j)需要质因数的个数(含重复)相差1,因此上图是一个基于质因数个数奇偶性的二分图。因此直接跑费用流即可。
AC代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<set>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#define inf 1000000000
#define N 20005
#define M 100005
#define ll long long
using namespace std;
int n,m,tot=1,gol,cnt,fst[N],pnt[M],len[M],nxt[M],p[M],h[M+5];
int a[N],b[N],c[N],blg[N],fa[N]; ll now,d[N],cst[M]; bool bo[M];
void add(int x,int y,int z,ll w){
pnt[++tot]=y; len[tot]=z; cst[tot]=w; nxt[tot]=fst[x]; fst[x]=tot;
}
void ins(int x,int y,int z,ll w){
add(x,y,z,w); add(y,x,0,-w);
}
void pfs(){
int i,j; memset(bo,1,sizeof(bo));
for (i=2; i<=32000; i++){
if (bo[i]) p[++cnt]=i;
for (j=1; j<=cnt && i*p[j]<=32000; j++){
bo[i*p[j]]=0;
if (!(i%p[j])) break;
}
}
}
int calc(int x){
int t=0,i;
for (i=1; i<=cnt && x>1; i++) if (!(x%p[i]))
for (; !(x%p[i]); x/=p[i]) t++;
if (x>1) t++; return t;
}
bool spfa(){
int i,head=0,tail=1; h[1]=0;
for (i=1; i<=gol; i++) d[i]=-((ll)inf*inf);
memset(bo,1,sizeof(bo[0])*(gol+1));
while (head!=tail){
head=head%M+1; int x=h[head],p; bo[x]=1;
for (p=fst[x]; p; p=nxt[p]) if (len[p]){
int y=pnt[p];
if (d[x]+cst[p]>d[y]){
d[y]=d[x]+cst[p]; fa[y]=p;
if (bo[y]){
bo[y]=0; tail=tail%M+1; h[tail]=y;
}
}
}
}
return d[gol]>-((ll)inf*inf);
}
int update(){
int tmp=inf,i;
for (i=gol; i; i=pnt[fa[i]^1]) tmp=min(tmp,len[fa[i]]);
if (now+(ll)tmp*d[gol]<0) tmp=now/(-d[gol]);
for (i=gol; i; i=pnt[fa[i]^1]){
len[fa[i]]-=tmp; len[fa[i]^1]+=tmp;
}
now+=(ll)tmp*d[gol]; return tmp;
}
int main(){
scanf("%d",&n); int i,j; pfs();
for (i=1; i<=n; i++) scanf("%d",&a[i]);
for (i=1; i<=n; i++) scanf("%d",&b[i]);
for (i=1; i<=n; i++) scanf("%d",&c[i]);
gol=n+1;
for (i=1; i<=n; i++){
blg[i]=calc(a[i])&1;
if (blg[i]) ins(0,i,b[i],0); else ins(i,gol,b[i],0);
}
for (i=1; i<=n; i++)
for (j=1; j<=n; j++) if (!(a[i]%a[j]) && calc(a[i]/a[j])==1){
if (blg[i]) ins(i,j,inf,(ll)c[i]*c[j]);
else ins(j,i,inf,(ll)c[i]*c[j]);
}
int ans=0,flow;
while (spfa()){
flow=update(); if (!flow) break;
ans+=flow;
}
printf("%d\n",ans);
return 0;
}
by lych
2016.4.21