4514: [Sdoi2016]数字配对

4514: [Sdoi2016]数字配对

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 1082   Solved: 410
[ Submit][ Status][ Discuss]

Description

有 n 种数字,第 i 种数字是 ai、有 bi 个,权值是 ci。
若两个数字 ai、aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数,
那么这两个数字可以配对,并获得 ci×cj 的价值。
一个数字只能参与一次配对,可以不参与配对。
在获得的价值总和不小于 0 的前提下,求最多进行多少次配对。

Input

第一行一个整数 n。
第二行 n 个整数 a1、a2、……、an。
第三行 n 个整数 b1、b2、……、bn。
第四行 n 个整数 c1、c2、……、cn。

Output

 一行一个数,最多进行多少次配对

Sample Input

3
2 4 8
2 200 7
-1 -2 1

Sample Output

4

HINT

 n≤200,ai≤10^9,bi≤10^5,∣ci∣≤10^5

Source

[ Submit][ Status][ Discuss]



要注意到。。能发生配对关系的两个数,所含有的质因数个数一定相差1

所以含有奇数个质因数的数归一类,含偶数个的一类,这样就能构造出一张二分图

先筛好小素数,然后O(n^2)判断每一对能否配对,能的话就连边

但是题中有个限制,花费不能低于0。也就是说要保证最大流的同时花费最大

将所有花费取反,就能够用最小费用最大流解决了

注意每次找到的增广路都是现有所有增广路中最优的一条(花费最小)

如果当前花费累计后总花费大于0,那往后也就不会有增广路了,所以直接特判退出


写的时候c[i]*c[j]没用long long。。。没特判数字1(质因数个数要为0)。。。连边前没检查能否整除。。

都是很蠢的错误啊。。。估计是打了一下午游戏的缘故= =

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

typedef long long LL;
const int maxn = 222;
const int maxm = maxn*maxn*2;
const int INF = ~0U>>1;
const LL inf = 1E16;
const int N = 1E5 + 10;

struct E{
	int to,cap,flow; LL cost; E(){}
	E(int to,int cap,int flow,LL cost): to(to),cap(cap),flow(flow),cost(cost){}
}edgs[maxm];

int n,tot,S,T,cnt,pri[N],a[maxn],b[maxn],c[maxn],siz[maxn],from[maxn],flow[maxn];
LL cost[maxn];
bool not_pri[N],vis[maxn];

vector <int> v[maxn],Num[maxn];
queue <int> Q;

void Add(int x,int y,int cap,LL cost)
{
	v[x].push_back(cnt); edgs[cnt++] = E(y,cap,0,cost);
	v[y].push_back(cnt); edgs[cnt++] = E(x,0,0,-cost);
}

bool pass(int x,int y)
{
	if (a[x] < a[y]) swap(x,y);
	if (a[x] % a[y] != 0) return 0;
	int z = a[x] / a[y];
	for (int i = 0; i < Num[x].size(); i++)
		if (Num[x][i] == z) return 1;
	return 0;
}

bool SPFA()
{
	for (int i = S; i <= T; i++) cost[i] = inf;
	Q.push(S); vis[S] = 1; cost[S] = 0; flow[S] = INF;
	while (!Q.empty())
	{
		int k = Q.front(); Q.pop(); vis[k] = 0;
		for (int i = 0; i < v[k].size(); i++)
		{
			E e = edgs[v[k][i]];
			if (e.cap == e.flow) continue;
			if (cost[e.to] > cost[k] + e.cost)
			{
				cost[e.to] = cost[k] + e.cost;
				flow[e.to] = min(flow[k],e.cap - e.flow);
				from[e.to] = v[k][i];
				if (!vis[e.to]) vis[e.to] = 1,Q.push(e.to);
			}
		}
	}
	return cost[T] != inf;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	for (int i = 2; i < N; i++)
	{
		if (!not_pri[i]) pri[++tot] = i;
		for (int j = 1; j <= tot; j++)
		{
			int Nex = pri[j]*i;
			if (Nex >= N || not_pri[Nex]) break;
			not_pri[Nex] = 1;
		}
	}
	
	cin >> n; T = n + 1;
	for (int i = 1; i <= n; i++)
	{
		scanf("%d",&a[i]);
		if (a[i] == 1) continue;
		int g = sqrt(a[i]),z = a[i];
		for (int j = 1; j <= tot; j++)
		{
			if (pri[j] > g)
			{
				Num[i].push_back(z);
				++siz[i]; break;
			}
			if (z % pri[j] == 0)
			{
				Num[i].push_back(pri[j]);
				while (z % pri[j] == 0) ++siz[i],z /= pri[j];
			}
			if (z == 1) break;
		}
	}
	for (int i = 1; i <= n; i++) scanf("%d",&b[i]);
	for (int i = 1; i <= n; i++) scanf("%d",&c[i]);
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if ((siz[i]&1) && !(siz[j]&1) && pass(i,j))
				Add(i,j,INF,-1LL*c[i]*c[j]);
	for (int i = 1; i <= n; i++)
		if (siz[i]&1) Add(S,i,b[i],0);
		else Add(i,T,b[i],0);
	
	LL Ans,sum; Ans = sum = 0;
	while (SPFA())
	{
		if (sum + 1LL*flow[T]*cost[T] > 0)
		{
			Ans += -sum/cost[T];
			break;
		}
		else
		{
			Ans += flow[T]; sum += 1LL*flow[T]*cost[T];
			for (int z = T; z != S; z = edgs[from[z]^1].to)
			{
				edgs[from[z]].flow += flow[T];
				edgs[from[z]^1].flow -= flow[T];
			}
		}
	}
	cout << Ans;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值