有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; }