cf498C 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.
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 the answer for the problem.
3 2
8 3 8
1 2
2 3
0
3 2
8 12 8
1 2
2 3
2 恶补网络流中
把数字奇偶分开,显然询问是1奇1偶的,那么相当于在二分图上搞了
枚举每一个质因数,建图,网络流。没了。拆点都不要
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#define LL long long
#define inf 0x3ffffff
#define S 0
#define T 99999
#define N 200010
using namespace std;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
struct edge{int to,next,v;}e[10*N];
int head[N];
int a[N],h[N],q[N],u[N],v[N];
int n,m,cnt=1,ans;
inline void ins(int u,int v,int w)
{
e[++cnt].v=w;
e[cnt].to=v;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void insert(int u,int v,int w)
{
ins(u,v,w);
ins(v,u,0);
}
inline bool bfs()
{
int t=0,w=1;
memset(h,-1,sizeof(h));
q[1]=S;h[S]=0;
while (t<w)
{
int now=q[++t];
for (int i=head[now];i;i=e[i].next)
if (e[i].v&&h[e[i].to]==-1)
{
h[e[i].to]=h[now]+1;
q[++w]=e[i].to;
}
}
if (h[T]==-1)return 0;
return 1;
}
inline int dfs(int x,int f)
{
if (x==T||!f)return f;
int w,used=0;
for (int i=head[x];i;i=e[i].next)
if (e[i].v&&h[e[i].to]==h[x]+1)
{
w=dfs(e[i].to,min(e[i].v,f-used));
e[i].v-=w;
e[i^1].v+=w;
used+=w;
if (f==used)return f;
}
if (!used)h[x]=-1;
return used;
}
inline void dinic(){while (bfs())ans+=dfs(S,inf);}
inline void solve(int x)
{
cnt=1;
memset(head,0,sizeof(head));
for (int i=1;i<=n;i++)
{
int t=0;
while (a[i]%x==0)t++,a[i]/=x;
if(i&1)insert(S,i,t);
else insert(i+n,T,t);
}
for (int i=1;i<=m;i++)insert(u[i],v[i]+n,inf);
dinic();
}
int main()
{
n=read();m=read();
for (int i=1;i<=n;i++)a[i]=read();
for (int i=1;i<=m;i++)
{
u[i]=read();
v[i]=read();
if (u[i]%2==0)swap(u[i],v[i]);
}
for (int i=1;i<=n;i++)
{
int t=sqrt(a[i]);
for (int j=2;j<=t;j++)if (a[i]%j==0)solve(j);
if (a[i]!=1)solve(a[i]);
}
printf("%d\n",ans);
return 0;
}
cf498C Array and Operations的更多相关文章
- CF498C. Array and Operations [二分图]
CF498C. Array and Operations 题意: 给定一个长为 n 的数组,以及 m 对下标 (a, b) 且满足 a + b 为奇数,每次操作可以将同一组的两个数同时除以一个公约数 ...
- 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 ...
- Array and Operations
A. Array and Operations Time Limit: 1000ms Memory Limit: 262144KB 64-bit integer IO format: %I64d ...
- 网络流(最大流):CodeForces 499E 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 goo ...
- Codeforces 498C Array and Operations(最大流)
题目是给一些数和<数对>的下标,然后进行操作:对某个<数对>中的两个数同时除以一个都能被它们整除且不等于1的数,要求的就是最多能进行多少次操作. 除数一定是素数,就是要决定某素 ...
- codeforcese 498C. Array and Operations 网络流
题目链接 给n个数, m个数对, 每个数对是两个下标加起来为奇数的两个数.每次操作可以使一个数对中的两个数同时除某个数, 除的这个数是这两个数的任意约数, 问这种操作最多可以做几次.n<100, ...
- Codeforces Round #284 (Div. 1) C. Array and Operations 二分图匹配
因为只有奇偶之间有操作, 可以看出是二分图, 然后拆质因子, 二分图最大匹配求答案就好啦. #include<bits/stdc++.h> #define LL long long #de ...
- ZOJ 3427 Array Slicing (scanf使用)
题意 Watashi发明了一种蛋疼(eggache) 语言 你要为这个语言实现一个 array slicing 函数 这个函数的功能是 有一个数组初始为空 每次给你一个区间[ l, r) 和 ...
- Codeforces Round #284 (Div. 1)
A. Crazy Town 这一题只需要考虑是否经过所给的线,如果起点和终点都在其中一条线的一侧,那么很明显从起点走点终点是不需要穿过这条线的,否则则一定要经过这条线,并且步数+1.用叉积判断即可. ...
随机推荐
- POJ Farm Tour
Farm Tour 题目: 约翰有N块地,家在1号,而N号是个仓库.农场内有M条道路(双向的),道路i连接这ai号地和bi号地,长度为ci. 约翰希望依照从家里出发,经过若干地后达到仓库.然后再返回家 ...
- Java基础知识强化70:正则表达式之引入案例(QQ号码校验)
1. 校验QQ号码的案例,如下: package cn.itcast_01; import java.util.Scanner; /* * 校验qq号码. * 1:要求必须是5-15位数字 * 2:0 ...
- 国内使用google地图的初级使用
<!DOCTYPE html><html><head><title>Simple Map</title><meta name=&quo ...
- 织梦DEDECMS {dede:field name='position'/}标签增加其它属性的
在默认情况下,织梦(DedeCms)系统当前位置的调用标签为: {dede:field name='position'/} 在这种默认的情况下,生成后的代码大致为如下格式: 主页 > 应用软件 ...
- apache的500错误是写到哪个文件里面
apache的500错误是写到哪个文件里面
- java多线程心得
多并发的时候,在什么情况下必须加锁?如果不加锁会产生什么样的后果. 加锁的场景跟java的new thread和Runnable的关系是什么? 看看java的concurrentMap源码. 还有sp ...
- ORACLE的CONNECT和RESOURCE角色权限
最近在处理数据库的用户权限问题,之前惯性思维,觉得给用户授权RESOURCE权限之后,用户的一般权限都会有,等到发现用户有RESOURCE角色,却没有创建视图的权限之后,才发现这部分还是一知半解啊,所 ...
- 如何往IE工具条添加按钮(转载)
如何往IE工具条添加按钮 问题提出:金山词霸.网络蚂蚁等软件安装后会向IE的工具条添加自己的按钮.按下按钮后还会作出相应的动作,这种功能是如何实现的呢?读完本文,您也可以将自己应用程序的按钮添加到IE ...
- Spring4.0学习笔记(2) —— 自动装配
Spring IOC 容器可以自动装配Bean,需要做的是在<bean>的autowire属性里指定自动装配的模式 1)byType 根据类型自动装配,若IOC 容器中有多个目标Bean类 ...
- KACK的处理方法
demo: .eq { color:#f00;/*标准浏览器*/ color:#f30\0;/*IE8,IE9,opera*/ *color:#c00;/*IE7及IE6*/ _color:#600; ...