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

Sample Output22

Input

第一行一个正整数n,表示数的个数。n<=3000
第二行n个正整数a1,a2,...an

Output

最大的和

Sample Input5
3 4 5 6 7

 
首先可以发现,只有奇数和偶数才可能满足上面两个条件;
那么我们把序列分为奇偶两部分;
设源点st,汇点ed;
st 和奇数连边,ed 和偶数连边;
那么我们遍历可能的连边,O(n^2);
这个连边权值设为 inf;
(有点最大权闭合子图的意思?
那么我们求最小割即可;
最后就是sum-dinic();
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 300005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll qpow(ll a, ll b, ll c) {
ll ans = 1;
a = a % c;
while (b) {
if (b % 2)ans = ans * a%c;
b /= 2; a = a * a%c;
}
return ans;
} int n, m;
int st, ed;
struct node {
int u, v, nxt, w;
}edge[maxn<<2]; int head[maxn], cnt;
void addedge(int u, int v, int w) {
edge[cnt].u = u; edge[cnt].v = v; edge[cnt].w = w;
edge[cnt].nxt = head[u]; head[u] = cnt++;
}
int rk[maxn]; int bfs() {
queue<int>q;
ms(rk);
rk[st] = 1; q.push(st);
while (!q.empty()) {
int tmp = q.front(); q.pop();
for (int i = head[tmp]; i != -1; i = edge[i].nxt) {
int to = edge[i].v;
if (rk[to] || edge[i].w <= 0)continue;
rk[to] = rk[tmp] + 1; q.push(to);
}
}
return rk[ed];
} int dfs(int u, int flow) {
if (u == ed)return flow;
int add = 0;
for (int i = head[u]; i != -1; i = edge[i].nxt) {
int v = edge[i].v;
if (rk[v] != rk[u] + 1 || !edge[i].w)continue;
int tmpadd = dfs(v, min(edge[i].w, flow - add));
if (!tmpadd) { rk[v] = -1; continue; }
edge[i].w -= tmpadd; edge[i ^ 1].w += tmpadd; add += tmpadd;
}
return add;
} int ans;
void dinic() {
while (bfs())ans += dfs(st, inf);
} int a[maxn]; bool check(int a, int b) {
if(a % 2 == 1 && b % 2 == 1)return 0;
if (gcd(a, b) != 1)return 0;
ll sum = a * a + b * b; int p = (int)sqrt(sum);
if (p*p != sum)return 0;
return 1;
} int main()
{
//ios::sync_with_stdio(0);
memset(head, -1, sizeof(head));
rdint(n);int sum = 0;
for (int i = 1; i <= n; i++)rdint(a[i]), sum += a[i];
st = n + 1; ed = st + 1;
for (int i = 1; i <= n; i++) {
if (a[i] & 1)addedge(st, i, a[i]), addedge(i, st, 0);
else addedge(i, ed, a[i]), addedge(ed, i, 0);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
if (check(a[i], a[j])) {
if (a[i] & 1)addedge(i, j, inf), addedge(j, i, 0);
else addedge(j, i, inf), addedge(i, j, 0);
}
}
}
dinic();
cout << sum - ans << endl;
return 0;
}

Number BZOJ3275 最大流的更多相关文章

  1. c++ 设计模式6 (Decorator 装饰模式)

    4. “单一职责”类模式 在软件组件的设计中,如果责任划分的不清晰,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任. 典型模式代表: Decorato ...

  2. [GeekBand] 面向对象的设计模式(C++)(1)

    一个模式描述了一个在我们周围不断重复发生的问题,以及该问题的解决方案的核心. 这样,你就能一次又一次 地使用该方案而不必做重复劳动. 0. 从面向对象谈起 底层思维与抽象思维: 底层思维要求程序员&q ...

  3. Nginx http2.0

    109/110 HTTP2.0协议 优势必须使用TLS加密 传输数据量大幅减少 1:以二进制格式传输  2:标头压缩(header做压缩) 多路复用及相关功能 : 消息优先级 (比如样式表先渲染页面那 ...

  4. Practical Node.js (2018版) 13章, Node HTTP/2 Servers

    新增的章节. If you are not using HTTP/2, then you are losing out on big improvements. HTTP/2相比http/1有很大的区 ...

  5. 设计模式---单一职责模式之装饰模式(Decorator)

    前提:"单一职责"模式 在软件组件的设计中,如果责任划分的不清晰,使用继承,得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任 典型模式(表现 ...

  6. HTML5 本地文件操作之FileSystemAPI整理(一)

    一.请求配额 DeprecatedStorageInfo对象 window.webkitStorageInfo:当使用持久存储模式时需要用到该对象的接口 方法: 1.requestQuota(type ...

  7. C++设计模式 之 “单一职责”模式:Decorator、Bridge

    part 1 “单一职责”模式 在软件组件的设计中,如果责任划分的不清晰,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任. 典型模式 Decorato ...

  8. Spark机器学习9· 实时机器学习(scala with sbt)

    1 在线学习 模型随着接收的新消息,不断更新自己:而不是像离线训练一次次重新训练. 2 Spark Streaming 离散化流(DStream) 输入源:Akka actors.消息队列.Flume ...

  9. 学习记录:《C++设计模式——李建忠主讲》4.“单一职责”模式

    单一职责模式:在软件组件的设计中,如果责任划分的不清晰,使用继承得到的结果往往是随着需求的变化,子类急剧膨胀,同时充斥着重复代码,这时候的关键是划清责任. 典型模式:装饰模式(Decorator).桥 ...

随机推荐

  1. python 函数相关定义

    1.为什么要使用函数? 减少代码的冗余 2.函数先定义后使用(相当于变量一样先定义后使用) 3.函数的分类: 内置函数:python解释器自带的,直接拿来用就行了 自定义函数:根据自己的需求自己定义的 ...

  2. CSS——position

    position是指元素的定位方式,有:static.absolute.fixed.relative.inherit 5种. static 默认,布局排版方式按照HTML代码的顺序布局. absolu ...

  3. 11-15SQLserver基础--数据库之范式理论

    数据库的设计理论与思路 在设计数据库的时候,有一个著名的设计理论---范式理论. 1.内容: 第一范式:每一列的数据类型要单一,必须要统一: 第二范式:在设计主键的时候,主键尽量更能体现表中的数据信息 ...

  4. 10-20C#基础---一维、二维数组&&冒泡排序

    一.一维数组 1.定义:是某一种数据类型的数据的组合,数组用来分组基本类型或相同类型的对象.数组中的实体叫做数组的元素或成员. 2. 格式:int[ ] shuzu=new int[ 6];存放int ...

  5. 问题:oracle 字符串转换成日期;结果:[oracle] to_date() 与 to_char() 日期和字符串转换

    to_date("要转换的字符串","转换的格式")   两个参数的格式必须匹配,否则会报错. 即按照第二个参数的格式解释第一个参数. to_char(日期,& ...

  6. leetcode590

    树的后序遍历. class Solution { public: vector<Node> Tree; void postTree(Node node) { for (auto n : n ...

  7. leetcode429

    这道题目是属于树的层次遍历,使用两层的队列非空判断. class Solution { public: vector<vector<int>> levelOrder(Node* ...

  8. 部署和调优 3.1 dns安装配置-1

    安装配置DNS服务器 装一个bind,首先搜一下. yum list |grep bind bind.x86_64   我们安装这个 安装 yum install bind.x86_64 -y 看一下 ...

  9. 用JS,求斐波那契数列第n项的值

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  10. 201671010127 2016—2017-2 java学习新征程

    通过大一整个学年对Python和C语言的学习,我对编程的感受有了更进一步的认识.随着时代的进步,编程语言也在实时更新,面对越来越多的编程语言,对于在编程方面的初学者,选择一门适合自己的编程语言就显得十 ...