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. javascript闭包分析

    闭包是什么?闭包是Closure,简而言之,闭包就是: 闭包就是函数的局部变量集合,只是这些局部变量在函数返回后会继续存在. 闭包就是就是函数的“堆栈”在函数返回后并不释放,我们也可以理解为这些函数堆 ...

  2. PHP金字塔的输出

    相信学习语言的最初的时候,学到循环的时候,开始一定有种摸不着头脑,想砸电脑的冲动吧 这里就是记录我当初学习的时候,为了通过这个循环,学习的金字塔的输出 1.首先,要了解一个金字塔的输出就要去看它的表达 ...

  3. 监听EditText的变化

    http://liangruijun.blog.51cto.com/3061169/729505 之前博客上的有关EditText的文章,只是介绍EditText的一些最基本的用法,这次来深入学习一下 ...

  4. windows server 2003 系统重装蓝屏

    错误码:0X0000007B 这个代码和硬盘有关系,不过不用害怕,不是有坏道了,是设置问题或者病毒造成的硬盘引导分区错误.如果您在用原版系统盘安装系统的时候出这个问题,那说明您的机器配置还是比较新的, ...

  5. [转载]Oracle基础知识

    一.oracle安装过程略 二.sys用户和system用户 (1)sys用户是超级用户,具有最高权限,具有sysdba角色,有create database的权限 默认密码是change_onins ...

  6. C语言之分配

    #include "stdio.h" void main() { ] = {,,}; *sizeof(int));//malloc calloc relloc arr2[] = ; ...

  7. UIKit Animation

    UIKit Animation 1.属性动画 - (void)changeFrameAnimation { [UIView beginAnimations:@"frameAnimation& ...

  8. CABasicAnimation添加动画离开屏幕就动画停止的问题

    解决方法: animation.removedOnCompletion = NO;

  9. IOS 在IOS6中设置navigationBar背景图片 会有一条 黑色阴影 --- 解决方案

    //给navigationBar设置背景图片 if ([self.navigationController.navigationBar respondsToSelector:@selector(set ...

  10. 能用存储过程的DBHelper类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...