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.用叉积判断即可. ...
随机推荐
- 36th成都区域赛网络赛 hdoj4039 The Social Network(建图+字符串处理)
这题是某年成都区域赛网络赛的一题. 这题思路非常easy,可是从时间上考虑,不妨不要用矩阵存储,我用的链式前向星. 採用线上查询.利用map对字符串编号,由于非常方便.要推荐的朋友,事实上就是朋友的朋 ...
- SpringMVC04controller中定义多个方法
public class MyController extends MultiActionController { // 新增 方法修饰符要是public public ModelAndView ad ...
- CSS3 边框
说明:CSS3完全向后兼容,因此不必改变现有的设计.浏览器通常支持CSS2 CSS3模块 CSS3被划分为模块: 选择器 框模型 背景和边框 文本效果 2D/3D 转换 动画 多列布局 用户界面 CS ...
- Linux误删C基本运行库libc.so.6急救方法
首先普及一下关于libc.so.6的基本常识: libc.so.6是glibc的软链接 ll /lib64/libc.so.6lrwxrwxrwx 1 root root 11 Aug 27 201 ...
- C#垃圾回收
析构方法: 我们知道引用类型都有构造方法(constructor),相对应的也有一个析构方法(destructor).顾名思义,构造方法,就是在创建这个对象时,要执行的方法.例如,我们可以通过构造方法 ...
- 武汉科技大学ACM :1009: 华科版C语言程序设计教程(第二版)习题6.11
Problem Description n个人围成一圈,依次从1至n编号.从编号为1的人开始1至k报数,凡报数为k的人退出圈子,输出最后留下的一个人原来的编号. Input 首先输入一个t,表示有t组 ...
- c++模板类被继承时他的成员不能被子类看到
c++模板类被继承时他的成员不能被子类看到,必须用限定的符号 this->foo 或者 baseclass::foo,或者using bassclass::foo. msvc不提示错误,gcc ...
- Eclipse导入JavaWeb项目报错:The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path
JavaWeb项目中写的JSP页面需要Web容器解析处理成HTML才能展示到前端浏览器,解析JSP需要Web容器.JSP页面顶端出现“红色”的报错信息:The superclass "jav ...
- JS实现定时器(类似工行网银支付限时操作)
js脚本内容: //5秒倒计时 var num = 0 ; var max = 5 ; var id = null ; id = setInterval(box , 1000) ; //1秒钟调用 ...
- 输入框 input只能输入正数和小数点
输入框 input只能输入正数和小数点 限制文本框只能输入正数,负数,小数 onkeyup="value=value.replace(/[^\-?\d.]/g,'')" 限制文本 ...