codeforces 498C Array and Operations

http://www.elijahqi.win/2018/01/09/codeforces-498c-array-and-operations/
C. Array and Operations
time limit per test

1 second
memory limit per test

256 megabytes
input

standard input
output

standard 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.
Examples
Input

3 2
8 3 8
1 2
2 3

Output

0

Input

3 2
8 12 8
1 2
2 3

Output

2

orz 囧rz 囧rz icefox怎么辣么强啊%%%

我好菜啊 要是智商有他这么高就好啦qwq

这题题意:给定一些有关联的数 然后因为他们有关联 所以我每次都能给他们进行同除 要求必须整除 好 现在求我针对这样的一种操作我最多能做多少次 首先可以考虑到的就是我先对原数质因数分解一下 那么 每次只有同除质因数才有可能保证是最多的次数

所以呢 我暴力枚举质因数 发现某个数是质因数 我就做一遍 首先在每个数都尽可能除掉 然后记录下他们能被除的次数 然后如果两个数能够匹配 那么他们中间连一条inf的边 如果是偶数就放在左边 奇数就放在右边 然后 跑最大流 就是我最大流就是我要的答案累积起来就是答案


#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define inf 0x3f3f3f3f
#define N 110
using namespace std;
inline char gc(){
    static char now[1<<16],*S,*T;
    if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
    return *S++;
}
inline int read(){
    int x=0;char ch=gc();
    while(ch<'0'||ch>'9') ch=gc();
    while(ch<='9'&&ch>='0') x=x*10+ch-'0',ch=gc();
    return x;
}
struct node{
    int y,z,next;
}data[N<<3];
int h[N],cur[N],level[N],a[N],st[N],ed[N],num=1,T,cnt[N],n,m,ans;
inline void insert1(int x,int y,int z){
    data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;
    data[++num].y=x;data[num].z=0;data[num].next=h[y];h[y]=num;
}
inline bool bfs(){
    queue<int>q;q.push(0);memset(level,0,sizeof(level));level[0]=1;
    while(!q.empty()){
        int x=q.front();q.pop();
        for (int i=h[x];i;i=data[i].next){
            int y=data[i].y,z=data[i].z;
            if(level[y]||!z) continue;level[y]=level[x]+1;q.push(y);if (y==T) return 1; 
        }
    }return 0;
}
inline int dfs(int x,int s){
    if (x==T) return s;int ss=s;
    for (int &i=cur[x];i;i=data[i].next){
        int y=data[i].y,z=data[i].z;
        if (level[x]+1==level[y]&&z){
            int xx=dfs(y,min(z,s));if (!xx) level[y]=0;
            s-=xx;data[i].z-=xx;data[i^1].z+=xx;if(!s) return ss;
        }
    }return ss-s;
} 
inline void solve(int x){
    memset(h,0,sizeof(h));memset(cnt,0,sizeof(cnt));num=1;
    for (int i=1;i<=n;++i){
        while(a[i]%x==0) a[i]/=x,++cnt[i];if (!cnt[i]) continue;
        if (i&1) insert1(i,T,cnt[i]);else insert1(0,i,cnt[i]);
    }
    for (int i=1;i<=m;++i){
        if (cnt[st[i]]&&cnt[ed[i]]) insert1(st[i],ed[i],inf);
    } 
    while(bfs()) memcpy(cur,h,sizeof(cur)),ans+=dfs(0,inf);
}
int main(){
    freopen("cf.in","r",stdin);
    n=read();m=read();T=n+1;
    for (int i=1;i<=n;++i) a[i]=read();
    for (int i=1;i<=m;++i) {st[i]=read(),ed[i]=read();if (st[i]&1) swap(st[i],ed[i]);}
    for (int i=1;i<=n;++i){
        int nn=sqrt(a[i]);
        for (int j=2;j<=nn;++j) if(a[i]%j==0) solve(j);if (a[i]!=1) solve(a[i]);
    }printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值