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 28 3 81 22 3

Output
0

Input
3 28 12 81 22 3

Output
2  将点拆成多个素数,然后套用网络流,还有一种思路是建很多次图,分开处理素数的匹配。
  1 #include <iostream>
  2 #include <cstring>
  3 #include <cstdio>
  4 using namespace std;
  5 const int INF=1000000000;
  6 const int N=110,M=1010,K=35010;
  7 int P[K],cntx;bool check[K];
  8 void Linear_Shaker(){
  9     for(int i=2;i<K;i++){
 10         if(!check[i])P[++cntx]=i;
 11         for(int j=1;j<=cntx;j++){
 12             if(i*P[j]>=K)break;
 13             check[i*P[j]]=true;
 14             if(i%P[j]==0)break;
 15         }
 16     }
 17 }
 18 int v[N][12],c[N][12],id[N][12],h[N],idx;
 19 int cnt=1,fir[M],to[M*20],nxt[M*20],cap[M*20];
 20 int dis[M],gap[M],path[M],fron[M],q[M],f,b;
 21 void add(int a,int b,int c){
 22     nxt[++cnt]=fir[a];
 23     to[fir[a]=cnt]=b;
 24     cap[cnt]=c;
 25 }
 26
 27 void addedge(int a,int b,int c){
 28     //printf("%d %d\n",a,b);
 29     add(a,b,c);add(b,a,0);
 30 }
 31
 32 bool BFS(int S,int T){
 33     q[f=b]=T;dis[T]=1;
 34     while(f<=b){
 35         int x=q[f++];
 36         for(int i=fir[x];i;i=nxt[i])
 37             if(!dis[to[i]]){
 38                 dis[to[i]]=dis[x]+1;
 39                 q[++b]=to[i];
 40             }
 41     }
 42     return dis[S];
 43 }
 44
 45 int Max_Flow(int S,int T){
 46     if(!BFS(S,T))return 0;
 47     for(int i=S;i<=T;i++)fron[i]=fir[i];
 48     for(int i=S;i<=T;i++)gap[dis[i]]+=1;
 49     int ret=0,f,p=S;
 50     while(dis[S]<=T+1){
 51         if(p==T){
 52             f=INF;
 53             while(p!=S){
 54                 f=min(f,cap[path[p]]);
 55                 p=to[path[p]^1];
 56             }ret+=f,p=T;
 57             while(p!=S){
 58                 cap[path[p]]-=f;
 59                 cap[path[p]^1]+=f;
 60                 p=to[path[p]^1];
 61             }
 62         }
 63         for(int &i=fron[p];i;i=nxt[i])
 64             if(cap[i]&&dis[to[i]]==dis[p]-1){
 65                 path[p=to[i]]=i;break;
 66             }
 67         if(!fron[p]){
 68             if(!--gap[dis[p]])break;int Min=T+1;
 69             for(int i=fir[p];i;i=nxt[i])
 70                 if(cap[i])Min=min(Min,dis[to[i]]);
 71             gap[dis[p]=Min+1]+=1;fron[p]=fir[p];
 72             if(p!=S)p=to[path[p]^1];
 73         }
 74     }
 75     return ret;
 76 }
 77
 78 int n,m,S,T,num[N];
 79 int G[N][N];
 80 int main(){
 81     Linear_Shaker();
 82     scanf("%d%d",&n,&m);
 83     for(int i=1;i<=n;i++){
 84         scanf("%d",&num[i]);
 85         for(int j=1;j<=cntx;j++){
 86             if(P[j]*P[j]>num[i])break;
 87             if(num[i]%P[j]==0){
 88                 v[i][h[i]]=P[j];
 89                 while(num[i]%P[j]==0){
 90                     c[i][h[i]]+=1;
 91                     num[i]/=P[j];
 92                 }h[i]+=1;
 93             }
 94         }
 95         if(num[i]!=1){
 96             v[i][h[i]]=num[i];
 97             c[i][h[i]++]=1;
 98         }
 99     }
100     /*
101     for(int i=1;i<=n;i++){
102         for(int j=0;j<h[i];j++)
103             printf("<%d %d> ",v[i][j],c[i][j]);
104         puts("");
105     }
106     */
107     for(int i=1,a,b;i<=m;i++){
108         scanf("%d%d",&a,&b);
109         if(a%2)swap(a,b);
110         G[a][b]=1;
111     }
112     for(int i=1;i<=n;i++)
113         for(int j=0;j<h[i];j++)
114             id[i][j]=++idx;
115     S=0;T=idx+1;
116     for(int i=1;i<=n;i++)
117         for(int j=0;j<h[i];j++){
118             if(i%2==0)addedge(S,id[i][j],c[i][j]);
119             else addedge(id[i][j],T,c[i][j]);
120         }
121     for(int i=2;i<=n;i+=2)
122         for(int j=1;j<=n;j+=2)if(G[i][j])
123             for(int x=0;x<h[i];x++)
124                 for(int y=0;y<h[j];y++)
125                     if(v[i][x]==v[j][y])
126                         addedge(id[i][x],id[j][y],INF);
127     printf("%d\n",Max_Flow(S,T));
128     return 0;
129 }

转载于:https://www.cnblogs.com/TenderRun/p/5928206.html

网络流(最大流):CodeForces 499E Array and Operations相关推荐

  1. 【图论】网络流——最大流和最小费用流

    [图论]网络流--最大流和最小费用流 文章目录 [图论]网络流--最大流和最小费用流 1. 最大流问题 1.1 基本概念 1.2 寻求最大流的算法(Ford-Fulerson) 1.3 matlab求 ...

  2. 网络流最大流初步-Push–relabel maximum flow algorithm

    简介 做网络流最大流的题,常用的算法就是Dinic's algorithm.时间复杂度为,通常由于出题人水平较低,几乎能过所有的题.功利地看,这样就没问题了.但是,站在追求真(zhuang)理(B)的 ...

  3. POJ - 3614 Sunscreen(贪心/二分图最大匹配-多重匹配/网络流-最大流)

    题目链接:点击查看 题目大意:给出n头奶牛,奶牛们现在要晒太阳,每头奶牛需要[l,r]区间内的光照强度,现在有m种防晒霜,每种防晒霜可以让奶牛接受到val数值的光照强度,然后每种防晒霜只有num个,现 ...

  4. 1618D. Array and Operations

    D. Array and Operations:题目 之前写的了,直接上答案! #include <bits/stdc++.h> #define int long long #define ...

  5. 【bzoj1532】[POI2005]Kos-Dicing 二分+网络流最大流

    题目描述 Dicing 是一个两人玩的游戏,这个游戏在Byteotia非常流行. 甚至人们专门成立了这个游戏的一个俱乐部. 俱乐部的人时常在一起玩这个游戏然后评选出玩得最好的人.现在有一个非常不走运的 ...

  6. (通俗易懂小白入门)网络流最大流——EK算法

    网络流 网络流是模仿水流解决生活中类似问题的一种方法策略,来看这么一个问题,有一个自来水厂S,它要向目标T提供水量,从S出发有不确定数量和方向的水管,它可能直接到达T或者经过更多的节点的中转,目前确定 ...

  7. 网络流——最大流EK算法讲解

    网络流--最大流EK算法讲解 好了,这是第二篇博客了,如第一篇所述,来讲一讲刚刚理解的网络流.因为本人只会EK算法,所以先讲这个算法.(我会去补知识点的!!!) 什么是网络流??? 读者们刚接触这个知 ...

  8. CodeForces - 1312E Array Shrinking(区间dp)(通俗易懂)

    CodeForces - 1312E Array Shrinking(区间dp) 题目链接: 没做出来,看了一下别人的题解,才A掉.但网上没发现一篇讲得比较易懂的题解,所以就准备写一篇再加上我自己的理 ...

  9. 图论-网络流⑦-费用流解题

    图论-网络流⑦-费用流解题 上一篇:图论-网络流⑥-费用流 下一篇:图论-网络流⑧-有上下界的网络流 参考文献: https://www.luogu.com.cn/blog/user9012/solu ...

最新文章

  1. Zynq7000术语PL,PS,APU,SCU介绍
  2. Nodejs之WebSocket
  3. 计算机检测维修与数据恢复课件,2017年全国职业院校技能大赛中职组“计算机检测维修与数据恢复”赛项说明会ppt课件.ppt...
  4. 第 6-2 课:SpringMVC 核心 + 面试题
  5. Tomcat爆出高危漏洞
  6. PHP SESSION生存时间设置
  7. c# 多线程界面卡顿_优化electron客户端卡顿的几种方案
  8. openresty lua_package_path指令
  9. java集合框架之Collection
  10. 初探Object Pascal的类(七)
  11. GNU glibc 爆 gethostbyname 缓冲区溢出漏洞
  12. java编写一个web 留言板_Java Web在线留言板
  13. win7录屏_录屏软件推荐用什么?绝地求生录屏游戏的工具分享
  14. SQL Server 监视(Monitoring)体系架构
  15. 如何搭建CA(向CA申请证书)
  16. 淘宝店铺装修(Carousel - 旋转木马)
  17. 登陆和登录,login
  18. I/O函数 writel __raw_writel mb()/rmb()/wmb()
  19. XGBoost目标函数推导
  20. AWS、阿里云、Azure 云计算三巨头的“混战”

热门文章

  1. ASP.NET AjaxPro的应用 .AjaxPro使用中“XXX未定义”的一种解决方法(转载的)
  2. LeetCode(268)——缺失数字(JavaScript)
  3. mac php5.6 gd 扩展,mac 编译安装php5.6.40
  4. 我新买的GTX1050为何装上去一直是黑屏
  5. 为什么大家越来越不着急换手机?
  6. 旅游是开车自驾好还是坐火车好?能否从各个方面解答一下?
  7. 淘宝直播应关注哪些方面?
  8. 黄金为什么贵,黄金都有什么用处?
  9. 研究别人,能知道做什么赚钱,了解自己,能知道什么钱适合赚
  10. 计算机会比人的神经系统更发达吗?