codeforces498C
Array and Operations
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.
Examples
3 2
8 3 8
1 2
2 3
0
3 2
8 12 8
1 2
2 3
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的更多相关文章
随机推荐
- PHP基础介绍
php之基本操作 1.常用数据类型: 字符串.整形.浮点数.逻辑.数组.对象.NULL. 字符串: $x = "hello"; 整形:$x = 123; 浮点数:$x =1.123 ...
- 记一次项目上线后Log4j2不输出日志的坑
公司项目采用了Log4j2来输出日志,在开发环境和测试环境下均可以输出日志,但在生成环境就没有日志输出.开始毫无头绪,后来通过不断的排查,终于解决了这个问题.在此记录下该问题的解决过程,便于后 ...
- 《Python从菜鸟到高手》已经出版,开始连载了,购买送视频课程
好消息,<Python从菜鸟到高手>已经出版!!! JetBrains官方推荐图书!JetBrains官大中华区市场部经理赵磊作序!送2400分钟同步视频课程!500个案例,400道P ...
- [C#] LINQ之SelectMany
声明:本文为www.cnc6.cn原创,转载时请注明出处,谢谢! 一.第一种用法: public static IEnumerable<TResult> SelectMany<TSo ...
- RabbitMQ教程(二) ——linux下安装rabbitmq
安装过程参考官网: Installing on RPM-based Linux (RHEL, CentOS, Fedora, openSUSE) 首先需要安装erlang,参考:http://fedo ...
- Python-Django下载与基本命令
1.下载Django: pip3 install django 2.创建一个django project django-admin.py startproject mysite 当前目录下会生成mys ...
- Mysql 字符集及排序规则
一.字符集 字符集:就是用来定义字符在数据库中的编码的集合. 常见的字符集:utf8.Unicode.GBK.GB2312(支持中文).ASCCI(不支持中文) 二.字符集排序规则 作者本人用 ...
- php使用gd库输出中文内容的图片
正如标题所说那样,本文只讨论输出内容全部为中文或者包含中文的情况.如果内容全是字母或者其他字符的话,可以参考这篇博客:生成验证码 问题 此处要注意,标题中为什么要区别windows和linux分别实现 ...
- eclipse 中右键项目出现卡死导致无法共享项目的解决办法
亲身经历,这个问题出自于项目中的SVN地址不对,如果要更改SVN地址,可以断掉计算机的网,在eclipse的工作空间中找到该项目,找到隐藏的.svn 文件夹,删除掉之后,打开eclipse,此时就可以 ...
- sql之cursor的简介和字符串拆分(split)与游标的使用
字符串拆分(split)与游标的使用 CREATE TABLE Plates ( ,), ) NOT NULL, [BusinessId] INT NOT NULL, ) ),),), SELECT ...