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的更多相关文章
随机推荐
- day95
Linux基本部署配置及常见扩展应用 Linux软件包安装方法 1. 安装: 整个安装过程可以分为以下几步: 1) 取得应用软件:通过下载.购买光盘的方法获得: 2)解压缩文件:一般tar包,都会再做 ...
- FineUIMvc新特性速递(大间距模式,隐藏菜单垂直滚动条)
即将发布的 FineUIMvc 新版本会引入两个重要的特性,用来提升用户体验,现在就来先睹为快吧: 大间距模式 我们已经支持的显示模式有:紧凑模式,普通模式,大字体模式. 紧凑模式: 普通模式: 大字 ...
- WPF仿网易云音乐系列(序)
1.简介 由于之前做了一个播放器,苦于不懂界面设计,只得去借鉴借鉴一些成功的作品,网易云音乐就甚合朕心,哈哈,最后做出来的效果如下: 本系列文章就来和大家讨论以下,如何用WPF去仿制一个网易云音乐来: ...
- CF892.B. Wrath
---恢复内容开始--- 题意: 有n个犯人,手上都有个长度为Li的武器,当铃响时大家同时挥动武器,只能把前面攻击范围内的敌人杀死,问最后还剩几个人. 题目传送门: [http://codeforce ...
- abaqus安装破解
软件安装包 链接:http://pan.baidu.com/s/1pL4oxfX 密码:on1g 破解网页视频链接https://v.youku.com/v_show/id_XMTg4ODM5NjY5 ...
- CRM系统(第一部分)
阅读目录 1.需求分析 2.数据库表设计 3.起步 4.录入数据 5.知识点 1.需求分析 CRM客户关系管理软件---> 学员管理 用户:企业内部用户 用户量: 业务场景: 2.数据库表设 ...
- vue组件star开发基于vue-cli
<template> <div class="stars"> <div v-for="(item,ind) in num" :ke ...
- C#复习笔记(3)--C#2:解决C#1的问题(可空值类型)
可空值类型 C#2推出可空类型来表示可以为null的值类型.这是一个呼声很高的需求,因为在常用的数据库中都是允许某些值类型可为空的.那么为什么值类型就不能为空呢?内存中用一个全0的值来表示null,但 ...
- 12 Connections
1 and 出现在两个及以上的词,词组,或者句子之间,将它们连接起来.在正式的书面英语中,and不能出现在句首.在非正式的英语中可以. We should expand our product lin ...
- Day1 基础知识
数据类型,字符编码 二进制: 定义:二进制数据是用0和1两个数码来表示的数.它的基数为2,进位规则是“逢二进一”,借位规则是“借一当二”.当前的计算机系统使用的基本上是二进制系统,数据在计算机中主要是 ...