题目

有 n 种数字,第 i 种数字是 ai、有 bi 个,权值是 ci。

若两个数字 ai、aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数,

那么这两个数字可以配对,并获得 ci×cj 的价值。

一个数字只能参与一次配对,可以不参与配对。

在获得的价值总和不小于 0 的前提下,求最多进行多少次配对。

输入格式

第一行一个整数 n。

第二行 n 个整数 a1、a2、……、an。

第三行 n 个整数 b1、b2、……、bn。

第四行 n 个整数 c1、c2、……、cn。

输出格式

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

输入样例

3

2 4 8

2 200 7

-1 -2 1

输出样例

4

提示

n≤200,ai≤109,bi≤105,∣ci∣≤10^5

题解

如果\(a_i\)和\(a_j\)之间相差一个质数,那么它们一定是互为倍数关系且质因子分解后指数和只相差\(1\)

容易发现,相互匹配的两个数的指数和一定是一奇一偶

所以这是一个二分图匹配问题

用最大费用最大流实现匹配即可

但由于费用和非负,我们在最大流统计流量时,如果当前答案加上本次新增费用小于0,就刚好取到不小于0的部分

由于先取大的一定比取小的好,所以这样的贪心策略正确

#include<iostream>
#include<cstdio>
#include<cmath>
#include<queue>
#include<cstring>
#include<algorithm>
#define LL long long int
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define BUG(s,n) for (int i = 1; i <= (n); i++) cout<<s[i]<<' '; puts("");
using namespace std;
const int maxn = 205,maxm = 200005;
const LL INF = 1000000000000000000ll;
inline int read(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
int n,ans;
LL a[maxn],b[maxn],c[maxn],cnt[maxn],sum;
LL d[maxn],minf[maxn];
int p[maxn];
int h[maxn],ne = 2,S,T;
struct EDGE{int to,nxt; LL f,w;}ed[maxm];
inline void build(int u,int v,LL f,LL w){
ed[ne] = (EDGE){v,h[u],f,w}; h[u] = ne++;
ed[ne] = (EDGE){u,h[v],0,-w}; h[v] = ne++;
}
void Sp(int u){
int x = a[u];
for (int i = 2; i <= x; i++)
if (x % i == 0){
while (x % i == 0) cnt[u]++,x /= i;
}
if (x - 1) cnt[u]++;
}
queue<int> q;
int inq[maxn];
void mincost(){
while (true){
for (int i = S; i <= T; i++) inq[i] = p[i] = 0,d[i] = INF;
q.push(S); d[S] = 0; minf[S] = INF;
int u;
while (!q.empty()){
u = q.front(); q.pop();
inq[u] = false;
Redge(u) if (ed[k].f && d[to = ed[k].to] > d[u] + ed[k].w){
d[to] = d[u] + ed[k].w; minf[to] = min(minf[u],ed[k].f);
p[to] = k;
if (!inq[to]) q.push(to),inq[to] = true;
}
}
if (d[T] == INF) return;
if (d[T] <= 0){
sum += -d[T] * minf[T];
ans += minf[T];
u = T;
while (u != S){
ed[p[u]].f -= minf[T];
ed[p[u] ^ 1].f += minf[T];
u = ed[p[u] ^ 1].to;
}
}
else {
int l = min(minf[T],sum / d[T]);
if (!l) return;
sum += -d[T] * l;
ans += l;
u = T;
while (u != S){
ed[p[u]].f -= l;
ed[p[u] ^ 1].f += l;
u = ed[p[u] ^ 1].to;
}
}
}
}
int main(){
n = read(); S = 0; T = n + 1;
REP(i,n) a[i] = read(),Sp(i);
REP(i,n){
b[i] = read();
if (cnt[i] & 1) build(S,i,b[i],0);
else build(i,T,b[i],0);
}
REP(i,n) c[i] = read();
REP(i,n) REP(j,n){
if ((cnt[i] & 1) && !(cnt[j] & 1) && ((a[i] % a[j] == 0 && cnt[i] == cnt[j] + 1) || (a[j] % a[i] == 0 && cnt[j] == cnt[i] + 1))){
build(i,j,INF,-(c[i] * c[j]));
}
}
mincost();
printf("%d\n",ans);
return 0;
}

最新文章

  1. google closure--继承模块一:goog.inherits()
  2. jdk的内存设置
  3. 安卓+servlet+MySql 查询+插入(汉字乱码解决)
  4. Canvas与Image互相转换
  5. Programming Assignment 4: 8 Puzzle
  6. SQL Server中的事务日志管理(5/9):完整恢复模式里的日志管理
  7. android activity之间传递返回值
  8. Quartz Scheduler(2.2.1) - hello world
  9. django学习之Model(四)MakingQuery
  10. web服务器配置方法
  11. VS2012下systemC配置
  12. Linux下配置tomcat+apr+native应对高并发
  13. USB接口禁用与启用
  14. 导出html table 数据到Excel
  15. 20165312 2017-2018-2《JAVA程序设计》第8周学习总结
  16. [sql]sql函数coalesce返回第一个非空的值
  17. Oracle Applications Documentation
  18. 三种分布式对象主流技术——COM、Java和COBRA
  19. python3.4学习笔记(十五) 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
  20. Powershell笔记之help about_XXX

热门文章

  1. jQuery如何获取选中单选按钮radio的值
  2. hash join
  3. HomeKit开发(一)
  4. Matlab-plot绘图
  5. .pyc文件的结构体PyCodeObject
  6. php进行文件的强制下载
  7. Golang ioutil读写文件测试
  8. linux 下 docker-compose安装
  9. 896. Monotonic Array
  10. Python语言程序设计之一--for循环中累加变量是否要清零