BZOJ4514——[Sdoi2016]数字配对
#include <map>
#include <set>
#include <queue>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define LL long long
#define llinf 2000000000000000000
#define inf 2147483647
#define for1(i, x, y) for(LL i = (x); i <= (y); i ++)
#define for2(i, x, y) for(LL i = (x); i >= (y); i --)
namespace mcmf{
LL s, t;
struct Edge{
LL u, v, cap, flow;
LL cost;
LL next;
} G[250010];
LL tot;
LL head[3000];
LL inq[3000];
LL d[3000];
LL p[3000];
LL a[3000];
inline void init(){
memset(head, -1, sizeof(head));
tot = -1;
}
inline void add(LL u, LL v, LL w, LL cost){
G[++ tot] = (Edge){u, v, w, 0, cost, head[u]};
head[u] = tot;
G[++ tot] = (Edge){v, u, 0, 0, -cost, head[v]};
head[v] = tot;
return;
}
inline bool BellmanFord(LL& flow, LL& cost){
for(LL i = s; i <= t; i ++) d[i] = -llinf;
memset(inq, 0, sizeof(inq));
d[s] = 0;
inq[s] = 1;
p[s] = 0;
a[s] = inf;
queue<LL> Q;
Q.push(s);
while(!Q.empty()){
LL u = Q.front(); Q.pop();
inq[u] = 0;
for(LL i = head[u]; i != -1; i = G[i].next){
Edge& e = G[i];
if(e.cap > e.flow && d[e.v] < d[u] + e.cost){
d[e.v] = d[u] + e.cost;
p[e.v] = i;
a[e.v] = min(a[u], e.cap - e.flow);
if(!inq[e.v]){
Q.push(e.v);
inq[e.v] = 1;
}
}
}
}
if(d[t] == -llinf) return false;
flow += a[t];
cost += d[t] * (LL)a[t];
if(cost < 0){
cost -= d[t] * (LL)a[t];
flow -= a[t];
flow += cost / -d[t];
return false;
}
LL u = t;
while(u != s){
G[p[u]].flow += a[t];
G[p[u] ^ 1].flow -= a[t];
u = G[p[u]].u;
}
return true;
}
inline LL Minflow(){
LL flow = 0; LL cost = 0;
while(BellmanFord(flow, cost));
return flow;
}
}
LL a[100010], b[100010], c[100010];
LL prime[100010], tot; bool vis[100010];
LL cnta[100010];
inline LL read(){ // getchar较快于scanf,更快的还有fread,不会23333
char ch = getchar(); LL x = 0, f = 1;
while(ch < '0' || ch > '9'){
if(ch == '-') f = -1;
ch = getchar();
}
while('0' <= ch && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline LL llread(){
char ch = getchar(); LL x = 0, f = 1;
while(ch < '0' || ch > '9'){
if(ch == '-') f = -1;
ch = getchar();
}
while('0' <= ch && ch <= '9'){
x = x * 10 + ch - '0';
ch = getchar();
}
return x * f;
}
inline void init_prime(){
for1(i, 2, 100000){
if(!vis[i]) prime[++ tot] = i;
for1(j, 1, tot){
if(i * prime[j] > 100000) break;
vis[i * prime[j]] = 1;
if(i % prime[j] == 0) break;
}
}
}
inline bool is_prime(LL x){
if(x <= 100000) return 1 - vis[x];
LL t = sqrt(x);
for1(i, 1, tot){
if(prime[i] > t) break;
if(x % prime[i] == 0) return false;
}
return true;
}
int main(){
LL n = read();
for1(i, 1, n) a[i] = llread();
for1(i, 1, n) b[i] = llread();
for1(i, 1, n) c[i] = llread();
mcmf::init();
init_prime();
for1(i, 1, n){
LL t = sqrt(a[i]);
LL o = a[i];
for1(j, 1, tot){
if(prime[j] > t) break;
while(o % prime[j] == 0) o /= prime[j], cnta[i] ++;
if(o == 1) break;
}
if(o != 1) cnta[i] ++;
}
mcmf::s = 0; mcmf::t = n + 1;
for1(i, 1, n){
if(cnta[i] & 1) mcmf::add(0, i, b[i], 0);
else mcmf::add(i, n + 1, b[i], 0);
}
for1(i, 1, n) if(cnta[i] & 1){
for1(j, 1, n) if(!(cnta[j] & 1)){
if(a[i] % a[j] != 0 && a[j] % a[i] != 0) continue;
if(a[i] % a[j] == 0 && is_prime(a[i] / a[j])) mcmf::add(i, j, inf, c[i] * c[j]);
else if(a[j] % a[i] == 0 && is_prime(a[j] / a[i])) mcmf::add(i, j, inf, c[i] * c[j]);
}
}
printf("%lld", mcmf::Minflow());
return 0;
}
BZOJ4514——[Sdoi2016]数字配对的更多相关文章
- bzoj4514 [Sdoi2016]数字配对
Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对 ...
- BZOJ4514[Sdoi2016]数字配对——最大费用最大流
题目描述 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci ...
- bzoj4514 [Sdoi2016]数字配对(网络流)
Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对 ...
- [bzoj4514][SDOI2016]数字配对——二分图
题目描述 传送门 题解: 这个题真的是巨坑,经过了6个WA,2个TLE,1个RE后才终于搞出来,中间都有点放弃希望了... 主要是一定要注意longlong! 下面开始说明题解. 朴素的想法是: 如果 ...
- BZOJ4514 [Sdoi2016]数字配对 【费用流】
题目 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ai/aj 是一个质数, 那么这两个数字可以配对,并获得 ci×c ...
- bzoj4514: [Sdoi2016]数字配对--费用流
看了一眼题目&数据范围,觉得应该是带下界的费用流 原来想拆点变成二分图,能配对的连边,跑二分图,可行性未知 后来看到另外一种解法.. 符合匹配要求的数要满足:质因子的个数相差为1,且两者可整除 ...
- bzoj4514: [Sdoi2016]数字配对(费用流)
传送门 ps:费用流增广的时候费用和流量打反了……调了一个多小时 每个数只能参与一次配对,那么这就是一个匹配嘛 我们先把每个数分解质因数,记质因子总个数为$cnt_i$,那如果$a_i/a_j$是质数 ...
- 【bzoj4514】: [Sdoi2016]数字配对 图论-费用流
[bzoj4514]: [Sdoi2016]数字配对 好像正常的做法是建二分图? 我的是拆点然后 S->i cap=b[i] cost=0 i'->T cap=b[i] cost=0 然后 ...
- 【BZOJ4514】[Sdoi2016]数字配对 费用流
[BZOJ4514][Sdoi2016]数字配对 Description 有 n 种数字,第 i 种数字是 ai.有 bi 个,权值是 ci. 若两个数字 ai.aj 满足,ai 是 aj 的倍数,且 ...
随机推荐
- excel2013添加坐标轴名称label
图画好了,x.y轴没有名称,怎么办那 点击左上角有个---添加图标元素----里面有轴标题应该就是
- 细说Linux下软件包的安装与管理
一 源码安装方式 由于linux操作系统开放源代码,因而在其上安装的软件大部分也都是开源软件,例如apache.tomcat.php等软件.开源软件基本都提供源码下载,源码安装的方式:源码安 ...
- 视频直播APP开发分析
视频直播APP开发到目前为止都还是热门的一个行业,而且发展到现在直播的种类非常多,很多行业都打入了直播行业,再也不是单纯的人物直播这么单一了.视频直播APP开发行业就像是吃螃蟹,来的早的人不懂如何吃, ...
- JavaScript排序算法——归并排序
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- fileinput模块
刚才练习的时候,报如下错误: AttributeError: module 'fileinput' has no attribute 'input',后来Google参考这篇文章https://mai ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)--MySQL错误
MySQL错误整理: 错误一: ERROR (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/my ...
- vs2013 支持C#6.0 Install-Package Microsoft.Net.Compilers
vs2013 支持C#6.0 Install-Package Microsoft.Net.Compilers
- 谷歌浏览器 查看源码里的a:hover CSS样式 还有火狐的查看方式
谷歌浏览器查看hover css样式 直接实例: 选中a标签 然后右侧面板 出现 箭头 点击 出现Toggle Element state提示 下面再hover前打勾 直接就可以显示效果了 浏览器查看 ...
- OC-block
#import <Foundation/Foundation.h> /* block要掌握的东西 1> 如何定义block变量 int (^sumBlock)(int, int); ...
- Spring 事务传递教程_有实例
通过这篇文章,你将学习到Spring框架中中事务的传递 简介 在处理Spring管理的事务时,开发人员可以以传播的方式定义事务的行为.换句话说,开发人员能够决定业务方法如何被封装在逻辑和物理事务中.来 ...