codeforces498C
Array and Operations
You have written on a piece of paper an array of n positive integers a[1], a[2], ..., a[n] and m good pairs of integers (i1, j1), (i2, j2), ..., (im, jm). Each good pair (ik, jk) meets the following conditions: ik + jk is an odd number and 1 ≤ ik < jk ≤ n.
In one operation you can perform a sequence of actions:
- take one of the good pairs (ik, jk) and some integer v (v > 1), which divides both numbers a[ik] and a[jk];
- divide both numbers by v, i. e. perform the assignments:
and
.
Determine the maximum number of operations you can sequentially perform on the given array. Note that one pair may be used several times in the described operations.
Input
The first line contains two space-separated integers n, m (2 ≤ n ≤ 100, 1 ≤ m ≤ 100).
The second line contains n space-separated integers a[1], a[2], ..., a[n] (1 ≤ a[i] ≤ 109) — the description of the array.
The following m lines contain the description of good pairs. The k-th line contains two space-separated integers ik, jk (1 ≤ ik < jk ≤ n, ik + jk is an odd number).
It is guaranteed that all the good pairs are distinct.
Output
Output the answer for the problem.
Examples
3 2
8 3 8
1 2
2 3
0
3 2
8 12 8
1 2
2 3
2 sol:看到两个坐标相加一定是奇数,而且这个数据范围100,100,容易联想到网络流,而且分组就是下标奇偶分成两组。
建图就呼之欲出了,对于每个质因数建一张图,源点S向每个奇数下标连上那个数字中那个质因数个数,同理偶数下标向汇点T连边,对于奇偶之间就连上他们质因数个数的较小值
#include <bits/stdc++.h>
using namespace std;
typedef int ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=,M=,inf=0x3f3f3f3f;
int n,m,a[N],Ges[N];
int S,T;
struct Edge
{
int U,V;
}E[N];
int Prim[];
bool Bo[];
inline void Shai(int Up)
{
int i,j;
Bo[]=Bo[]=;
for(i=;i<=Up;i++)
{
if(!Bo[i]) Prim[++*Prim]=i;
for(j=;j<=*Prim&&i*Prim[j]<=Up;j++)
{
Bo[i*Prim[j]]=; if(i%Prim[j]==) break;
}
}
}
namespace Picture
{
int tot=,Next[M],to[M],Val[M],head[N]; inline void Init();
inline void add(int x,int y,int z);
inline bool bfs(int S);
inline int dfs(int S,int T,int Dist);
inline int Max_Flow(); inline void Init()
{
tot=;
memset(head,,sizeof head);
}
inline void add(int x,int y,int z)
{
Next[++tot]=head[x];
to[tot]=y;
Val[tot]=z;
head[x]=tot; Next[++tot]=head[y];
to[tot]=x;
Val[tot]=;
head[y]=tot;
}
int Depth[N];
inline bool bfs(int S)
{
memset(Depth,,sizeof Depth);
int i;
queue<int>Queue;
while(!Queue.empty()) Queue.pop();
Depth[S]=;
Queue.push(S);
while(!Queue.empty())
{
int x=Queue.front(); Queue.pop();
for(i=head[x];i;i=Next[i]) if(Val[i]>&&Depth[to[i]]==)
{
Depth[to[i]]=Depth[x]+;
Queue.push(to[i]);
}
}
return (Depth[T]==)?:;
}
inline int dfs(int x,int Dist)
{
if(x==T) return Dist;
int i;
for(i=head[x];i;i=Next[i])
{
if(Depth[to[i]]==Depth[x]+&&Val[i]>)
{
int oo=dfs(to[i],min(Dist,Val[i]));
if(oo>)
{
Val[i]-=oo;
(i&)?Val[i+]+=oo:Val[i-]+=oo;
return oo;
}
}
}
return ;
}
inline int Max_Flow()
{
int ans=;
while(bfs(S))
{
ans+=dfs(S,inf);
}
return ans;
}
}
#define Pic Picture
int main()
{
int i,j,ans=;
R(n); R(m);
S=; T=n+;
for(i=;i<=n;i++) R(a[i]);
for(i=;i<=m;i++)
{
R(E[i].U); R(E[i].V);
if(E[i].U%==) swap(E[i].U,E[i].V);
}
Shai();
for(i=;i<=*Prim;i++)
{
memset(Ges,,sizeof Ges);
Pic::Init();
for(j=;j<=n;j++)
{
while(a[j]%Prim[i]==)
{
a[j]/=Prim[i]; Ges[j]++;
}
}
for(j=;j<=n;j++)
{
if(j&) Pic::add(S,j,Ges[j]);
else Pic::add(j,T,Ges[j]);
}
for(j=;j<=m;j++)
{
Pic::add(E[j].U,E[j].V,min(Ges[E[j].U],Ges[E[j].V]));
}
ans+=Pic::Max_Flow();
}
Pic::Init();
for(i=;i<=n;i++) if(a[i]!=)
{
if(i&) Pic::add(S,i,);
else Pic::add(i,T,);
}
for(i=;i<=m;i++) if(a[E[i].U]==a[E[i].V]&&a[E[i].U]!=)
{
Pic::add(E[i].U,E[i].V,);
}
ans+=Pic::Max_Flow();
Wl(ans);
return ;
}
/*
Input
3 2
8 3 8
1 2
2 3
Output
0 Input
3 2
8 12 8
1 2
2 3
Output
2
*/
codeforces498C的更多相关文章
随机推荐
- iScroll.js 向上滑动异步加载数据回弹问题
iScroll是一款用于移动设备web开发的一款插件.像缩放.下拉刷新.滑动切换等移动应用上常见的一些效果都可以轻松实现. 现在最新版本是5.X,官网这里:http://iscrolljs.com/ ...
- bootstrap 失效的原因
首先必须对着官方文档的模板来写: https://getbootstrap.com/docs/4.3/getting-started/introduction/ 然后: 这里必须加上 rel=&quo ...
- 几种事务的隔离级别,InnoDB如何实现?
事务ACID特性,其中I代表隔离性(Isolation). 什么是事务的隔离性? 隔离性是指,多个用户的并发事务访问同一个数据库时,一个用户的事务不应该被其他用户的事务干扰,多个并发事务之间要相互隔离 ...
- 未安装Oracle客户端的服务器上,使用ASP.NET远程连接Oracle
公司服务器一直都是使用 .NET+SQLSERVER的开发方式,有个项目需要进行读取远程Oracle的需求.由于oracle 基本不会,也是一边做一遍摸索. 首先是使用工具测试是否已经联通,因为之前用 ...
- Django signals 信号作用及用法说明
参考:https://docs.djangoproject.com/en/1.11/ref/signals/ 1.Model signals django.db.models.signales 作用于 ...
- 剑指offer--1.二维数组中的查找
题目:在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...
- Yii框架的增删改查总结分析
一.查询数据 1.findAll(根据一个条件查询一个集合) $admin=Admin::model()->findAll($condition,$params); $admin=Admin:: ...
- C#使用OneNote的图片文字识别功能(OCR)
http://www.cnblogs.com/Charltsing/p/OneNoteOCR.html 有需要技术咨询的,联系QQ564955427 前段时间有人问我能不能通过OneNote扫描图片, ...
- Windows 下 Mysql8.0.12 的安装方法
1. 之前在windows 上面安装了 mysql 5.6 还有 mysql 5.7 遇到了几个坑 , 最近想直接安装最新版的 mysql 8.0.12(较新) 发现还是有坑 跟之前的版本不一样 这里 ...
- 同一个机器 安装多个版本Chrome浏览器的方法
1. Chrome 现在安装直接没有任何提示 就直接安装了 而且自动式 高版本覆盖低版本安装 不给你任何选择版本的机会. 2. 但是chrome 的安装是基于用户的 所以 同一个机器 使用不同的用户 ...