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.

 

Sample Input

Input
3 2
8 3 8
1 2
2 3
Output
0
Input
3 2
8 12 8
1 2
2 3
Output
2
  将点拆成多个素数,然后套用网络流,还有一种思路是建很多次图,分开处理素数的匹配。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int INF=;
const int N=,M=,K=;
int P[K],cntx;bool check[K];
void Linear_Shaker(){
for(int i=;i<K;i++){
if(!check[i])P[++cntx]=i;
for(int j=;j<=cntx;j++){
if(i*P[j]>=K)break;
check[i*P[j]]=true;
if(i%P[j]==)break;
}
}
}
int v[N][],c[N][],id[N][],h[N],idx;
int cnt=,fir[M],to[M*],nxt[M*],cap[M*];
int dis[M],gap[M],path[M],fron[M],q[M],f,b;
void add(int a,int b,int c){
nxt[++cnt]=fir[a];
to[fir[a]=cnt]=b;
cap[cnt]=c;
} void addedge(int a,int b,int c){
//printf("%d %d\n",a,b);
add(a,b,c);add(b,a,);
} bool BFS(int S,int T){
q[f=b]=T;dis[T]=;
while(f<=b){
int x=q[f++];
for(int i=fir[x];i;i=nxt[i])
if(!dis[to[i]]){
dis[to[i]]=dis[x]+;
q[++b]=to[i];
}
}
return dis[S];
} int Max_Flow(int S,int T){
if(!BFS(S,T))return ;
for(int i=S;i<=T;i++)fron[i]=fir[i];
for(int i=S;i<=T;i++)gap[dis[i]]+=;
int ret=,f,p=S;
while(dis[S]<=T+){
if(p==T){
f=INF;
while(p!=S){
f=min(f,cap[path[p]]);
p=to[path[p]^];
}ret+=f,p=T;
while(p!=S){
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
}
for(int &i=fron[p];i;i=nxt[i])
if(cap[i]&&dis[to[i]]==dis[p]-){
path[p=to[i]]=i;break;
}
if(!fron[p]){
if(!--gap[dis[p]])break;int Min=T+;
for(int i=fir[p];i;i=nxt[i])
if(cap[i])Min=min(Min,dis[to[i]]);
gap[dis[p]=Min+]+=;fron[p]=fir[p];
if(p!=S)p=to[path[p]^];
}
}
return ret;
} int n,m,S,T,num[N];
int G[N][N];
int main(){
Linear_Shaker();
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&num[i]);
for(int j=;j<=cntx;j++){
if(P[j]*P[j]>num[i])break;
if(num[i]%P[j]==){
v[i][h[i]]=P[j];
while(num[i]%P[j]==){
c[i][h[i]]+=;
num[i]/=P[j];
}h[i]+=;
}
}
if(num[i]!=){
v[i][h[i]]=num[i];
c[i][h[i]++]=;
}
}
/*
for(int i=1;i<=n;i++){
for(int j=0;j<h[i];j++)
printf("<%d %d> ",v[i][j],c[i][j]);
puts("");
}
*/
for(int i=,a,b;i<=m;i++){
scanf("%d%d",&a,&b);
if(a%)swap(a,b);
G[a][b]=;
}
for(int i=;i<=n;i++)
for(int j=;j<h[i];j++)
id[i][j]=++idx;
S=;T=idx+;
for(int i=;i<=n;i++)
for(int j=;j<h[i];j++){
if(i%==)addedge(S,id[i][j],c[i][j]);
else addedge(id[i][j],T,c[i][j]);
}
for(int i=;i<=n;i+=)
for(int j=;j<=n;j+=)if(G[i][j])
for(int x=;x<h[i];x++)
for(int y=;y<h[j];y++)
if(v[i][x]==v[j][y])
addedge(id[i][x],id[j][y],INF);
printf("%d\n",Max_Flow(S,T));
return ;
}

网络流(最大流):CodeForces 499E Array and Operations的更多相关文章

  1. Codeforces 498C Array and Operations(最大流)

    题目是给一些数和<数对>的下标,然后进行操作:对某个<数对>中的两个数同时除以一个都能被它们整除且不等于1的数,要求的就是最多能进行多少次操作. 除数一定是素数,就是要决定某素 ...

  2. Codeforces Round #284 (Div. 1) C. Array and Operations 二分图最大匹配

    题目链接: http://codeforces.com/problemset/problem/498/C C. Array and Operations time limit per test1 se ...

  3. cf498C Array and Operations

    C. Array and Operations time limit per test 1 second memory limit per test 256 megabytes input stand ...

  4. Array and Operations

    A. Array and Operations Time Limit: 1000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d    ...

  5. POJ 1459-Power Network(网络流-最大流-ISAP)C++

    Power Network 时间限制: 1 Sec  内存限制: 128 MB 题目描述 A power network consists of nodes (power stations, cons ...

  6. CF498C. Array and Operations [二分图]

    CF498C. Array and Operations 题意: 给定一个长为 n 的数组,以及 m 对下标 (a, b) 且满足 a + b 为奇数,每次操作可以将同一组的两个数同时除以一个公约数 ...

  7. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

  8. HDU 3081 Marriage Match II (网络流,最大流,二分,并查集)

    HDU 3081 Marriage Match II (网络流,最大流,二分,并查集) Description Presumably, you all have known the question ...

  9. HDU1532 网络流最大流【EK算法】(模板题)

    <题目链接> 题目大意: 一个农夫他家的农田每次下雨都会被淹,所以这个农夫就修建了排水系统,还聪明的给每个排水管道设置了最大流量:首先输入两个数n,m ;n为排水管道的数量,m为节点的数量 ...

随机推荐

  1. Table显示滚动条

    Table显示滚动条,要先把table放到一个div中,div的长度和宽度要固定,控制overflow属性为scroll <div style="width:700px; height ...

  2. [JS] JavascriptHelp (转载)

    using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...

  3. SQL-Server索引漫谈

    http://www.cnblogs.com/teroy/archive/2013/05/23/3070547.html

  4. linux导入导出数据库方法 windows导入导出数据库方法

    1.使用管理员账号(sys)登录查询字符集信息 第一步:查询LinuxOracle数据库的字符集 select userenv('language') from dual; 查询结果集可能为:AMER ...

  5. 【html】【3】html标签列表

    必看参考: http://www.divcss5.com/html/h323.shtml http://www.w3school.com.cn/tags/tag_html.asp 常用: <ht ...

  6. linux命令sed学习笔记

    sed其实就是两个主要的知识点,那就是“怎么选择”和“怎么操作”!

  7. Java学习----finally块

    public class Test { String x; public static void main(String[] args) { Test test = new Test(); try { ...

  8. 《深入.NET平台和C#编程》内部测试题-笔试试卷

    1.以下关于序列化和反序列化的描述错误的是( C). a.序列化是将对象的状态存储到特定存储介质中的过程 b.二进制格式化器的Serialize()和Deserialize()方法可以用来实现序列化和 ...

  9. execute、executeUpdate、executeQuery三者的区别及返回值

    一.boolean execute(String sql)允许执行查询语句.更新语句.DDL语句.返回值为true时,表示执行的是查询语句,可以通过getResultSet方法获取结果:返回值为fal ...

  10. awk 多分隔符

    #!/bin/bash log_path="./log/" dates=`date -d '-1 days' +'%Y%m%d'` cd $log_path; for i in ` ...