Codeforces498C解题报告
2015-05-16 17:28
337 查看
C. Array and Operations
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have written on a piece of paper an array of n positive integers a[1], a[2], …, a
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
(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.
Sample test(s)
input
3 2
8 3 8
1 2
2 3
output
0
input
3 2
8 12 8
1 2
2 3
output
2
Solution:
考虑如何建图,由于“好”点对的和为一个奇数,不难想到(ui,vi)中ui与vi分别是一个奇数和一个偶数,然后建成二分图:暴力枚举因子数,相邻的u,v连无容量限制的边,从原点向奇数点集、偶数点集向汇点连出因子数的边,然后最大流为题目的解。
Code:
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You have written on a piece of paper an array of n positive integers a[1], a[2], …, a
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
(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.
Sample test(s)
input
3 2
8 3 8
1 2
2 3
output
0
input
3 2
8 12 8
1 2
2 3
output
2
Solution:
考虑如何建图,由于“好”点对的和为一个奇数,不难想到(ui,vi)中ui与vi分别是一个奇数和一个偶数,然后建成二分图:暴力枚举因子数,相邻的u,v连无容量限制的边,从原点向奇数点集、偶数点集向汇点连出因子数的边,然后最大流为题目的解。
Code:
#include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; #define maxn 200 + 10 #define maxm 40000 + 10 #define INF 0x7f7f7f7f int n,m,s = 0,t, ans = 0,cur = 1, front[maxn], que[maxn], h[maxn], a[maxn], u[maxn], v[maxn]; struct edge{ int to, next, cap; }e[maxm]; inline void addedge(int u, int v, int c){ cur ++; e[cur].to = v; e[cur].next = front[u]; e[cur].cap = c; front[u] = cur; cur ++; e[cur].to = u; e[cur].next = front[v]; e[cur].cap = 0; front[v] = cur; } inline bool bfs(){ int x, tp = 0, d = 1; memset(h, -1, sizeof(h)); que[tp] = h[0] = 0; while(tp < d){ x = que[tp]; tp ++; int now = front[x]; while(now){ if(e[now].cap && h[e[now].to] < 0){ que[d ++] = e[now].to; h[e[now].to] = h[x] + 1; } now = e[now].next; } } return h[t] == -1 ? 0 : 1; } inline int dfs(int x, int min_adv){ if(x == t) return min_adv; int now = front[x]; int flow = 0, f; while(now){ if(e[now].cap && h[e[now].to] == h[x] + 1){ f = min_adv - flow; f = dfs(e[now].to, min(f, e[now].cap)); e[now].cap -= f; e[now^1].cap += f; flow += f; if(flow == min_adv) return min_adv; } now = e[now].next; } if(!flow) h[x] = -1; return flow; } inline void dinic(){ while(bfs()) ans += dfs(0, INF); } inline void calc(int x){ cur = 1; memset(front, 0, sizeof(front)); for(int i = 1; i <= n; i ++){ int tot = 0; while(!(a[i] % x)) a[i] /= x, tot ++; if(i & 1) addedge(s, i, tot); else addedge(n + i, t, tot); } for(int i = 1; i <= m; i ++)addedge(u[i], n + v[i], INF); dinic(); } int main(){ scanf("%d%d",&n,&m); t = n << 1 | 1; for(int i = 1; i <= n; i ++) scanf("%d",&a[i]); for(int i = 1; i <= m; i ++){ scanf("%d%d",&u[i], &v[i]); if(v[i] & 1) swap(u[i],v[i]); } for(int i = 1; i <= n; i ++){ for(int j = 2; j <= sqrt(a[i]); j ++) if(!(a[i] % j)) calc(j); if(a[i] != 1) calc(a[i]); } printf("%d\n",ans); return 0; }
相关文章推荐
- 解题报告 HDU1176 免费馅饼
- 单词方阵解题报告
- 【LeetCode】80.Remove Duplicates from Sorted Array II(Medium)解题报告
- poj 3335-Rotating Scoreboard解题报告
- Codeforces Round #284 (Div. 2) C. Crazy Town ACM解题报告(几何)
- LeetCode 1. Two Sum 解题报告
- [leetcode] 453. Minimum Moves to Equal Array Elements 解题报告
- BestCoder27 1002.Taking Bus(hdu 5163) 解题报告
- 深圳大学2012年程序设计比赛解题报告
- [解题报告]hdoj1069(动态规划)
- [Leetcode] 319. Bulb Switcher 解题报告
- hdoj 1269-迷宫城堡解题报告
- 寒假训练5解题报告
- 解题报告:POJ_1338 Ugly Numbers
- 华东交通大学2013ACM“双基”程序设计竞赛 解题报告
- [leetcode] 229. Majority Element II 解题报告
- BZOJ 大视野 1001 狼抓兔子 解题报告
- noip2013提高组 积木大赛解题报告
- BestCoder Round #2 解题报告
- POJ1975解题报告-Floyd解决传递闭包