Codeforces498C解题报告

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[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.

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;
}
Codeforces 1806C题目链接:https://codeforces.com/problemset/problem/1806/C 题目描述: 给定一个长度为 $n$ 的整数序列 $a_1,a_2,\dots,a_n$,你可以进行任意多次操作,每次操作可以选择一个区间 $[l,r]$,将区间内的所有数加上 $1$ 或者减去 $1$。你的目标是使得序列中的所有数都相等,求最小的操作次数。 解题思路: 首先考虑对于一个数来说,如何将它变成序列中的所有数。由于每次操作可以将一个区间内的数全部加上或减去 $1$,所以我们可以将该数变成序列中的中位数。中位数是序列中所有数排完序后处在中间的数,如果序列长度为偶数,则中位数是中间两个数的平均数。 接下来考虑对于整个序列来说,如何将所有数变成相等的数。我们可以先将所有数变成中位数,然后计算每个数与中位数的差值之和,这个值就是最小的操作次数。 最后考虑如何将一个数变成中位数。设序列长度为 $n$,$x$ 为中位数,则有以下两种情况: - 当 $n$ 为奇数时,$x=a_{\lfloor\frac{n+1}{2}\rfloor}$。 - 当 $n$ 为偶数时,$x=\frac{a_{\frac{n}{2}}+a_{\frac{n}{2}+1}}{2}$。 对于第一种情况,我们可以将序列中所有数先减去 $a_{\lfloor\frac{n+1}{2}\rfloor}$,然后再进行操作。 对于第二种情况,我们可以将序列中所有数先减去 $\frac{a_{\frac{n}{2}}+a_{\frac{n}{2}+1}}{2}$,然后再进行操作。注意,当 $n=2$ 时,$\frac{n}{2}=1$,需要特判。 代码实现:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值