codeforcese 498C. Array and Operations 网络流
给n个数, m个数对, 每个数对是两个下标加起来为奇数的两个数。每次操作可以使一个数对中的两个数同时除某个数, 除的这个数是这两个数的任意约数, 问这种操作最多可以做几次。n<100, m<100。
这个数据范围, 以及下标加起来为奇数...好明显的网络流暗示。
首先对每个数分解, 看它含有哪些素因子, 每个素因子的个数是多少, 因为只有100个数, 所以素因子的总数不是很多。
然后枚举每个素因子。对每个数对中的两个数连边, 如果下标是奇数, 源点就向这个数连边, 下标是偶数, 这个数向汇点连边。 权值为这个数含有这个素因子的个数。
然后跑很多遍网络流, 答案就是所有结果相加。
具体可以看代码
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
map <int, int> mp[], ma;
pll b[];
const int maxn = 2e4;
int q[maxn*], head[maxn*], dis[maxn/], s, t, num;
int vis[];
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*];
void init() {
num = ;
mem1(head);
mem(vis);
}
void add(int u, int v, int c) {
e[num] = node(v, head[u], c); head[u] = num++;
e[num] = node(u, head[v], ); head[v] = num++;
}
int bfs() {
mem(dis);
dis[s] = ;
int st = , ed = ;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+;
if(v == t)
return ;
q[ed++] = v;
}
}
}
return ;
}
int dfs(int u, int limit) {
if(u == t) {
return limit;
}
int cost = ;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>) {
e[i].c -= tmp;
e[i^].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -;
}
}
}
return cost;
}
int dinic() {
int ans = ;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
int main()
{
int n, m, x;
cin>>n>>m;
for(int i = ; i<n; i++) {
scanf("%d", &x);
for(int j = ; j*j<=x; j++) {
if(x%j==) {
mp[i][j] = ; //mp是看第i个数含有素数j的个数
ma[j] = ; //ma是存储这n个数所有的素因子
}
while(x%j==) {
x/=j;
mp[i][j]++;
}
}
if(x>) {
mp[i][x] = ;
ma[x] = ;
}
}
for(int i = ; i<m; i++) {
cin>>b[i].fi>>b[i].se;
b[i].fi--, b[i].se--;
}
auto it = ma.begin();
int ans = ;
while(it != ma.end()) {
int pri = it->first; //枚举所有素因子
init();
s = n+, t = n+;
for(int i = ; i<m; i++) {
if(b[i].fi%==) {
add(b[i].fi, b[i].se, inf);
if(!vis[b[i].fi]) {
add(s, b[i].fi, mp[b[i].fi][pri]); //如果这个数之前没出现过, 那么就向源点或者汇点连边。
vis[b[i].fi] = ;
}
if(!vis[b[i].se]) {
add(b[i].se, t, mp[b[i].se][pri]);
vis[b[i].se] = ;
}
} else {
add(b[i].se, b[i].fi, inf);
if(!vis[b[i].fi]) {
add(b[i].fi, t, mp[b[i].fi][pri]);
vis[b[i].fi] = ;
}
if(!vis[b[i].se]) {
add(s, b[i].se, mp[b[i].se][pri]);
vis[b[i].se] = ;
}
}
}
int tmp = dinic();
ans += tmp;
it++;
}
cout<<ans<<endl; }
codeforcese 498C. Array and Operations 网络流的更多相关文章
- Codeforces 498C Array and Operations(最大流)
题目是给一些数和<数对>的下标,然后进行操作:对某个<数对>中的两个数同时除以一个都能被它们整除且不等于1的数,要求的就是最多能进行多少次操作. 除数一定是素数,就是要决定某素 ...
- cf498C Array and Operations
C. Array and Operations time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 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 Round #284 (Div. 1) C. Array and Operations 二分图匹配
因为只有奇偶之间有操作, 可以看出是二分图, 然后拆质因子, 二分图最大匹配求答案就好啦. #include<bits/stdc++.h> #define LL long long #de ...
- Codeforces 最大流 费用流
这套题目做完后,一定要反复的看! 代码经常出现的几个问题: 本机测试超时: 1.init函数忘记写. 2.addedge函数写成add函数. 3.边连错了. 代码TLE: 1.前向星边数组开小. 2. ...
- ZOJ 3427 Array Slicing (scanf使用)
题意 Watashi发明了一种蛋疼(eggache) 语言 你要为这个语言实现一个 array slicing 函数 这个函数的功能是 有一个数组初始为空 每次给你一个区间[ l, r) 和 ...
随机推荐
- python之lambda表达式
lambda函数小结 1.lambda表达式: 以前看人家写一个长式子就能干一件我写一个函数干的事情觉得好帅,现在通过学习知道了lambda表达式其原理就是一个函数,而且是一个只能处理简单功能的函数. ...
- bool operator==(const Array&)const; 这最后一个const 是做什么用的
字符重载也是个函数,在函数末尾加CONST 这样的函数叫常成员函数.常成员函数可以理解为是一个“只读”函数,它既不能更改数据成员的值,也不能调用那些能引起数据成员值变化的成员函数,只能调用const成 ...
- 学会用这二个键,你就是电脑高手了,一个是Win键,另一个是Ctrl!
学会用这二个键,你就是电脑高手了,一个是windows键,另一个是Ctrl键. 一.windows键 1. 很多时候,需要离开座位去做别的事情,如果对自己的电脑安全很重视,不妨按住windows键后, ...
- c# session总结
C# 中对 Session 的“(string)”.“.ToString()”与“Convert.ToString”用法笔记 在实际操作当中,我们经常会遇到将 Session 的值转为 String ...
- KVC中setValuesForKeysWithDictionary:
本文转载于:http://my.oschina.net/u/2407613/blog/524879?p={{page}} 从字典映射到一个对象,这是KVC中的一个方法所提供的,这个方法就是 setVa ...
- .NET软件开发与常用工具清单
[工欲善其事,必先利其器]软件开发的第一步就是选择高效.智能的工具. 下面列出的工具软件能辅助提高工作效率. 开发类工具 微软.Net平台下的集成开发环境:Visual Studio. Visual ...
- Windows主机和Linux虚拟机之间传输文件
如果使用VirtualBox的增强功能, 可以实现两者之间文件相互拖拽. 但某些情况下, 比如增强功能安装遇到难以解决的问题, 或者Linux版本为server版本(例如Ubuntu Server发行 ...
- 排序算法——交换排序(冒泡排序、快速排序)(java)
一.冒泡排序 时间复杂度:O(n^2) 公认最慢的排序,每次把最大/最小的放一边,原理: [57,68,59,52] [57,68,59,52] [57,59,68,52] [57,59,52,68] ...
- IOS 特定于设备的开发:检查设备接近度和电池状态
UIDevice类提供了一些API,使你能够跟踪设备的特征,包括电池的状态和接近度传感器.他们二者都以通知的形式提供更新,可以订阅他们,以便在有重要的更新时通知你的应用程序. 1>启动和禁用接近 ...
- 求1~n直接1出现的次数
参考前人的统计思想:分别统计个.十.百...亿等第N位上1出现的次数. 如ABCDE,在统计D位1出现的次数时,用D做分割符,ABC为Before,E为After. 分情况考虑:(n为D的length ...