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. Bootstrap学习(一):Bootstrap简介

    一.Bootstrap简介 Bootstrap,来自 Twitter,是目前最受欢迎的前端框架.Bootstrap 是基于 HTML.CSS.JAVASCRIPT 的,它简洁灵活,使得 Web 开发更 ...

  2. ASP.NET Core 企业开发架构概述

    企业开发框架包括垂直方向架构和水平方向架构.垂直方向架构是指一个应用程序的由下到上叠加多层的架构,同时这样的程序又叫整体式程序.水平方向架构是指将大应用分成若干小的应用实现系统功能的架构,同时这样的系 ...

  3. 三、xadmin----内置插件

    1.Action Xadmin 默认启用了批量删除的事件,代码见xadmin-->plugins-->action.py  DeleteSelectedAction 如果要为list列表添 ...

  4. Codeforces Round #486 (Div. 3)-B. Substrings Sort

    B. Substrings Sort time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  5. php微信公众号开发入门小教程

    1.配置相关服务器 (1) 如下,把自己的服务器ip白名单配置上: (2) 开始配置令牌,配置令牌时先需要把现成的代码放到自己的服务器上面,代码里面包含自己的设置的令牌号码,这样才可以配置成功. 注意 ...

  6. Python_内置函数之map()

    map 会根据提供的函数对指定序列做映射. 代码如下: def square(x): return x ** 2 ret = map(square, [1, 2, 3, 4, 5]) # 计算列表各元 ...

  7. 福州大学软件工程1816 | W班 第1次作业成绩排名

    1.作业地址 第一次作业--准备篇 2.作业要求 (1)回想一下你初入大学时对计算机专业的畅想 当初你是如何做出选择计算机专业的决定的? 你认为过去两年中接触到的课程是否符合你对计算机专业的期待,为什 ...

  8. Tomcat web.xml配置参数详解

    Apache Tomcat Configuration Reference - The Context Containerhttps://tomcat.apache.org/tomcat-5.5-do ...

  9. Non-Volatile Register 非易失性寄存器 调用约定对应寄存器使用

    非易失性寄存器(Non-volatile register)是它的内容必须通过子程序调用被保存的一个寄存器.如果一个程序改变了一个非易失性寄存器的值,它必须保存在改变这个寄存器之前堆栈中保存旧的值和在 ...

  10. js-XMLHttpRequest 2级

    ###1. XMLHttpRquest 2级 1)   FormData 现代web应用中频繁使用的一项功能就死表单数据的序列化, XMLHttpRquest 2级为此定义了FormData类型 Fo ...