3275: Number

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 558  Solved: 246
[Submit][Status][Discuss]

Description

有N个正整数,须要从中选出一些数。使这些数的和最大。

若两个数a,b同一时候满足下面条件,则a,b不能同一时候被选
1:存在正整数C,使a*a+b*b=c*c
2:gcd(a,b)=1

Input

第一行一个正整数n。表示数的个数。
第二行n个正整数a1,a2,?

an。

 
 

Output

最大的和。
 

Sample Input

5
3 4 5 6 7

Sample Output

22

HINT

n<=3000。

Source

网络流

我们能够发现仅仅有a和b奇偶性不同一时候才干满足题目条件。

然后对于全部奇数连边(s,i,a[i]),对于全部偶数连边(i,t,a[i])。对于满足题意的奇数i和偶数j连边(i,j,inf)。

最后跑一次最小割,从总和中减去最小割即为答案。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#define F(i,j,n) for(int i=j;i<=n;i++)
#define D(i,j,n) for(int i=j;i>=n;i--)
#define ll long long
#define pa pair<int,int>
#define maxn 3100
#define maxm 10000000
#define inf 1000000000
using namespace std;
struct edge_type
{int next,to,v;
}e[maxm];
int head[maxn],cur[maxn],dis[maxn],a[maxn];
int n,s,t,ans=0,tot=0,cnt=1;
inline int read()
{int x=0,f=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}return x*f;
}
inline void add_edge(int x,int y,int v)
{e[++cnt]=(edge_type){head[x],y,v};head[x]=cnt;e[++cnt]=(edge_type){head[y],x,0};head[y]=cnt;
}
inline bool bfs()
{queue<int>q;memset(dis,-1,sizeof(dis));dis[s]=0;q.push(s);while (!q.empty()){int tmp=q.front();q.pop();if (tmp==t) return true;for(int i=head[tmp];i;i=e[i].next) if (e[i].v&&dis[e[i].to]==-1){dis[e[i].to]=dis[tmp]+1;q.push(e[i].to);}}return false;
}
inline int dfs(int x,int f)
{if (x==t) return f;int tmp,sum=0;for(int &i=cur[x];i;i=e[i].next){int y=e[i].to;if (e[i].v&&dis[y]==dis[x]+1){tmp=dfs(y,min(f-sum,e[i].v));e[i].v-=tmp;e[i^1].v+=tmp;sum+=tmp;if (sum==f) return sum;}}if (!sum) dis[x]=-1;return sum;
}
inline void dinic()
{while (bfs()){F(i,1,t) cur[i]=head[i];ans+=dfs(s,inf);}
}
inline bool check(int x,int y)
{int tmp=x*x+y*y,rt=int(sqrt(tmp));if (rt*rt!=tmp) return false;if (x<y) swap(x,y);while (y) {tmp=x%y;x=y;y=tmp;}return (x==1);
}
int main()
{n=read();s=n+1;t=n+2;F(i,1,n){a[i]=read();tot+=a[i];if (a[i]&1) add_edge(s,i,a[i]);else add_edge(i,t,a[i]);}F(i,1,n) if (a[i]&1)F(j,1,n) if (!(a[j]&1))if (check(a[i],a[j])) add_edge(i,j,inf);dinic();printf("%d\n",tot-ans);
}

bzoj3275 Number相关推荐

  1. [BZOJ3275]Number解题报告|网络流

    Description 有N个正整数,需要从中选出一些数,使这些数的和最大. 若两个数a,b同时满足以下条件,则a,b不能同时被选 1:存在正整数C,使a*a+b*b=c*c 2:gcd(a,b)=1 ...

  2. [BZOJ3275]Number(最小割)

    题目描述 传送门 题解 很好想的最小割 将每个ai拆成xi,yi s->xi,ai yi->t,ai 对于不能同时选的两个点,xi->yi,INF 乱搞了一下很开心地A掉了,看hzw ...

  3. input属性为number,maxlength不起作用的解决方案

    <input type="text" maxlength="11" /> 效果ok, 当 <input type="number&q ...

  4. ORA-19502: write error on file xxxxx, block number xxxx

    错误现象: 在ORACLE 10g下为表空间IGNITE_EGVSQL01增加数据文件时,报如下错误: SQL> ALTER TABLESPACE IGNITE_EGVSQL01      AD ...

  5. LeetCode刷题记录10——434. Number of Segments in a String(easy)

    LeetCode刷题记录10--434. Number of Segments in a String(easy) 目录 LeetCode刷题记录9--434. Number of Segments ...

  6. HDU 1711 Number Sequence(KMP算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1711 Number Sequence Time Limit: 10000/5000 MS (Java/ ...

  7. LeetCode 191 Number of 1 Bits

    LeetCode 191 Number of 1 Bits 解法一(较为传统都解法):使用将n不断右移,并与1想&得到1的个数:(也有使用除法/2的,明显除法的运行效率要低于位移) 时间复杂度 ...

  8. [BuildRelease]build number / id

    build number, 也称为build id, 在build release的流程中唯一标示一个build,也是正式的产品的product version 和file version后两位(Ma ...

  9. Oracle-ORA-01722 invalid number错误

    本来正常的,经过抓包才知道原来是数字型的无意中多了乱码! 本来是3276的居然多了后面一串 3276PuAnrSeU2zliU+IV/FHlnX2Xgia1au2xX2vMWtw http://www ...

最新文章

  1. 掌握Redis分布式锁的正确姿势
  2. filebeat.yml配置和logstash.conf
  3. Access denied for user 'root'@'localhost' (using password: YES)的解决
  4. Java Web 三大框架开发资料
  5. 关于各种JOIN连接的解释说明【原创】
  6. java异常没有catch住_今天才真正了解Java的异常处理
  7. 都是❤️两层循环❤️的冒泡排序,选择排序,插入排序该怎么区分
  8. java 快速从树节点找到数据_数据结构与算法:单向链表和双向链表
  9. 地址栏引用PHP中变量,PHP 读取地址栏 参数
  10. 第四章:变量、作用域和内存问题
  11. 潍坊学院c语言上机题库,参编学校_C语言程序设计上机指导与同步训练(刘迎春、张艳霞)_pdf_大学课件预览_高等教育资讯网...
  12. C程序设计,贪吃蛇程序
  13. MKVToolNix Mac下载
  14. 关于6月20日PMP认证考试准考信下载及考场规定的通知
  15. 把计算机怎么连接手机的网络助手在哪里,手机如何连接电脑上网
  16. FTP无法在资源管理器中打开
  17. 3个阶段 项目征名_《传奇3G》新版本 征名活动邀您参与
  18. AID Learning设置aidcode的启动页面
  19. 康奈尔笔记法写程序笔记
  20. 【有奖倒计时】带你走进AI应用创新大赛十强背后的故事!

热门文章

  1. Python封装的获取文件目录的函数
  2. Numpy 模块的使用
  3. ansible之cron模块
  4. CSS 自动居中一列布局
  5. jmeter 一个可能引起性能严重下降的断言设置
  6. UIKit should not be called from a secondary thread.
  7. 【分治】LeetCode 50. Pow(x, n)
  8. 【LeetCode 148】链表的归并排序
  9. 基于Keras搭建mnist数据集训练识别的Pipeline
  10. python从语音生成语谱图