Array and Operations

CodeForces - 498C

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 nm (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 ikjk (1 ≤ ik < jk ≤ nik + jk is an odd number).

It is guaranteed that all the good pairs are distinct.

Output

Output the answer for the problem.

Examples

Input
3 2
8 3 8
1 2
2 3
Output
0
Input
3 2
8 12 8
1 2
2 3
Output
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的更多相关文章

随机推荐

  1. linux配置PS1

    自己常用的格式: vi ~/.bashrc export PS1="\[\e[31;1m\]\u@\[\e[34;1m\]\h \[\e[36;1m\]\W $\[\e[37;1m\] &q ...

  2. 【vue】vue-router跳转路径url多种格式

    1.形如  http://localhost:8080/#/book?id=**** ①路由配置 ②路由定向链接,以query传参id 另外,获取query传递的参数id用  this.$route. ...

  3. BZOJ 5467 Slay the Spire

    BZOJ 5467 Slay the Spire 我的概率基础也太差了.jpg 大概就是这样,因为强化牌至少翻倍,所以打出的牌必定是全部的强化牌或者$k-1$个强化牌,然后剩余的机会打出最大的几个攻击 ...

  4. 断路器(Curcuit Breaker)模式

    在分布式环境下,特别是微服务结构的分布式系统中, 一个软件系统调用另外一个远程系统是非常普遍的.这种远程调用的被调用方可能是另外一个进程,或者是跨网路的另外一台主机, 这种远程的调用和进程的内部调用最 ...

  5. 纯手写AJAX

    function ajax(){ //http相应对象 var xmlhttp; //判断浏览器 if(window.XMLHttpRequest){ xmlhttp = new XMLHttpReq ...

  6. python代码风格指南:pep8 中文版

    本文档所提供的编码规范,适用于主要的Python发行版中组成标准库的Python代码.请参阅PEP关于Python的C实现的C编码风格指南的描述. 本文档和PEP257(文档字符串规范)改编自Guid ...

  7. Mysql多实例添加到开机自启的方法

    Mysql多实例配置成功后,想让配置成开机自启. 首先看一下Linux启动的知识点,顺序如下. 1 加载内核2 执行init程序3 /etc/rc.d/rc.sysinit   # 由init执行的第 ...

  8. C#跨进程读取listview控件中的数据

    http://www.cnblogs.com/Charltsing/p/slv32.html 欢迎交流:QQ564955427 读取标准的32位listview控件中的数据,网上已经有很多代码了.今天 ...

  9. ES5中文分词(IK)

    ElasticSearch5中文分词(IK) ElasticSearch安装 官网:https://www.elastic.co 1.ElasticSearch安装 1.1.下载安装公共密钥 rpm ...

  10. JUnit的配置及使用

    一.安装插件JUnitGenertor V2.0 File->Setting->Plugins->在搜索框里输入JUintGenerator V2.0 二.导入JUnit相关jar包 ...