codeforces 498C C. Array and Operations(最大流+数论)
来源:互联网 发布:淘宝上的silkn是真的吗 编辑:程序博客网 时间:2024/05/18 02:43
题目链接:
codeforces 498C
题目大意:
给出一组数,然后给出他们之间的边,构成一张由奇数点和偶数点组成的二分图,然后存在边的数可以除以他们的公约数,问最多的操作次数。
题目分析:
- 为了得到了最多的操作次数,每次操作一定是除以两个数的公共的质因数。
- 所以我们对于每一个质因数建图,原图中的边的两个点都存在这个这个质因数的连边,边权为两个点这个质因数的数量中较小的那一个,奇数点与源建边,边权为它存在这个质因数的个数,偶数点同理,与汇建边,然后跑最大流。
- 因为通过筛素数,我们也就能筛出
105 以内的素数,对于大于这个边界的我们可以考虑,一个数(109 以内)最多只能存在一个105 以上的质因数,这个很容易想到,那么也就是最后我们对所有大于105 的质因数建一张图,对于原图中的边,只有两个点相等才建边,边权为1,(原数小于105
- 的质因数已经全部约去)
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#include <queue>#include <cmath>#define MAX 107#define INF (1<<29)using namespace std;int n,m,cnt,s,t;int a[MAX],x[MAX],y[MAX];vector<int> prime;int mark[100007];int num[MAX];int head[MAX],cc;struct Edge{ int v,next,w;}e[MAX*MAX];void init ( ){ memset ( mark , -1 , sizeof ( mark ) ); mark[1] = mark[0] = 0; prime.clear(); for ( int i = 2 ; i < 100007 ; i++ ) { if (~mark[i] )continue; prime.push_back ( i ); for ( int j = 2*i ; j < 100007 ; j += i ) mark[j] = i; }}void add ( int u , int v , int w ){ e[cc].v = v; e[cc].next = head[u]; e[cc].w = w; head[u] = cc++; e[cc].v = u; e[cc].next = head[v]; e[cc]. w = 0; head[v] = cc++;}int d[MAX];bool bfs ( int s , int t ){ queue<int> q; memset ( d , 0 , sizeof (d ) ); d[s] = 1; q.push(s); while ( !q.empty()) { int u = q.front(); q.pop(); if ( u == t ) return true; for ( int i = head[u] ; ~i ; i = e[i].next ) { int v = e[i].v; int w = e[i].w; if ( !d[v] && w ) { q.push ( v ); d[v] = d[u]+1; } } } return false;}int dfs ( int now , int maxf , int t ){ if ( now == t ) return maxf; int ret = 0,f; for ( int i = head[now] ; ~i ; i = e[i].next ) { int v = e[i].v; int w = e[i].w; if ( w && d[v] == d[now] + 1 ) { f = dfs ( v , min ( maxf - ret , w ) , t ); e[i].w -= f; e[i^1].w += f; ret += f; if ( ret == maxf ) return ret; } } return ret;}int dinic ( int s ,int t ){ int ans = 0; while ( bfs(s,t) ) ans += dfs ( s , INF , t ); return ans;}bool judge ( int num ){ if ( num <= sqrt ( 1e9 ) ) return false; for ( int i = 0 ; i < prime.size() && prime[i]*prime[i] <= num ; i++ ) if ( num%prime[i] == 0 ) return false; return true;}int main ( ){ init ( ); while ( ~scanf ( "%d%d" , &n , &m ) ) { s = 0 ,t = n+1; int ans = 0; for ( int i = 1; i <= n ; i++ ) scanf ( "%d" , &a[i] ); for ( int i = 0 ; i < m ; i++ ) { scanf ( "%d%d" , &x[i] , &y[i] ); if ( y[i]&1 ) swap ( x[i] , y[i] ); } int lim = sqrt ( 1e9 ); for ( int i = 2 ; i <= lim ; i++ ) { if ( ~mark[i] ) continue; memset ( head , -1 , sizeof ( head ) ); cc = 0; memset ( num , 0 , sizeof (num ) ); for( int j = 1 ; j <= n ; j++ ) { while ( a[j] % i == 0 ) { num[j]++; a[j] /= i; } } for ( int j = 1 ; j <= n ; j += 2 ) add ( s , j , num[j] ); for ( int j = 2 ; j <= n ; j += 2 ) add ( j , t , num[j] ); for ( int j = 0 ; j < m ; j++ ) { int w = min ( num[x[j]], num[y[j]] ); add ( x[j] , y[j] , w ); } ans += dinic ( s , t ); } memset ( head , -1 , sizeof ( head ) ); cc = 0; for ( int i = 1 ; i <= n ; i += 2 ) if ( judge ( a[i] ) ) add ( s , i , 1 ); for ( int i = 2 ; i <= n ; i += 2 ) if ( judge ( a[i] ) ) add ( i , t , 1 ); for ( int i = 0 ; i < m ; i++ ) if ( a[x[i]] != 1 && a[x[i]] == a[y[i]] ) add ( x[i] , y[i] , 1 ); ans += dinic ( s , t ); printf ( "%d\n" , ans ); }}
阅读全文
0 0
- codeforces 498C C. Array and Operations(最大流+数论)
- codeforces 498C C. Array and Operations(最大流+数论)
- CodeForces 498C Array and Operations(最大流)
- CodeForces 498C Array and Operations 最大流
- codeforces 498 c Array and Operations
- Codeforces 498C Array and Operations 题解
- 498C - Array and Operations 质因子分解+最大流
- codeforces 498C Array and Operations 网络流
- 【网络流】 codeforces 498C Array and Operations
- Codeforces Round #284 (Div. 1) C. Array and Operations(网络流‘最大流)
- Codeforces Round #284 (Div. 1) C. Array and Operations (最大流)
- Codeforces 489C. Array and Operations Hopcroft-Karp
- cf 498C Array and Operations 网络流
- 【CF#498C】Array and Operations 网络流
- Codeforces 718C C. Sasha and Array
- Codeforces #305 C. Mike and Frog 数论
- CodeForces 681C Heap Operations
- codeforces 681C Heap Operations
- postgres HBA基于主机的认证
- Window安装Redis并设置为开机启动
- 【JEECG】列表多表头的设计
- 51nod 1090 三个数和为0的问题
- mysql for mac修改初始密码
- codeforces 498C C. Array and Operations(最大流+数论)
- linux命令_系统管理_passwd
- python技巧一
- centos7配置yum源
- 从HGDB 1.3版本开始,瀚高数据库中添加了dual表
- SVN服务器搭建(保证一次成功配置)
- Win10 下安装 Android开发环境(AndroidStudio)
- 数据库的练习
- 作业二十八 单词拼接