Description
有N个正整数,需要从中选出一些数,使这些数的和最大。
若两个数a,b同时满足以下条件,则a,b不能同时被选
1:存在正整数C,使a*a+b*b=c*c
2:gcd(a,b)=1
Solution
带权最大独立集,考虑转化成二分图用最小割解决
注意到我们可以根据奇偶性黑白染色,即奇数和奇数不满足条件一,偶数和偶数不满足条件二,这两个集合内互不影响
这里还是证明一下性质一吧,两奇数的平方和可以这么表示
拆开
显然有 ,我们令 ,那么有
这是不成立的,然后就搞定了
Code
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))
typedef long long LL;
const int INF=0x7fffffff;
const int N=3005;
const int E=2200010;
struct edge {int y,w,next;} e[E];
std:: queue <int> que;
int dis[N],a[N];
int ls[N],edCnt=1;
void add_edge(int x,int y,int w) {
e[++edCnt]=(edge) {y,w,ls[x]}; ls[x]=edCnt;
e[++edCnt]=(edge) {x,0,ls[y]}; ls[y]=edCnt;
}
bool bfs(int st,int ed) {
for (;!que.empty();) que.pop();
rep(i,st,ed) dis[i]=0; dis[st]=1;
que.push(st);
for (;!que.empty();) {
int now=que.front(); que.pop();
for (int i=ls[now];i;i=e[i].next) {
if (e[i].w>0&&dis[e[i].y]==0) {
dis[e[i].y]=dis[now]+1;
que.push(e[i].y);
if (e[i].y==ed) return true;
}
}
}
return false;
}
int find(int now,int ed,int mn) {
if (now==ed||!mn) return mn;
int ret=0;
for (int i=ls[now];i;i=e[i].next) {
if (e[i].w>0&&dis[now]+1==dis[e[i].y]) {
int d=find(e[i].y,ed,std:: min(mn-ret,e[i].w));
e[i].w-=d; e[i^1].w+=d; ret+=d;
if (mn==ret) break;
}
}
return ret;
}
LL dinic(int st,int ed) {
LL ret=0;
for (;bfs(st,ed);) ret+=find(st,ed,INF);
return ret;
}
LL gcd(LL x,LL y) {
return !y?x:gcd(y,x%y);
}
bool check(LL a,LL b) {
LL tmp2=a*a+b*b;
LL tmp1=sqrt(tmp2); tmp1=tmp1*tmp1;
return (tmp1==tmp2&&gcd(a,b)==1);
}
int main(void) {
freopen("data.in","r",stdin);
freopen("myp.out","w",stdout);
int n; LL ans=0; scanf("%d",&n);
rep(i,1,n) {
scanf("%d",&a[i]); ans+=a[i];
if (a[i]&1) add_edge(0,i,a[i]);
else add_edge(i,n+1,a[i]);
}
rep(i,1,n) rep(j,1,n) {
if ((a[i]&1)&&(a[j]%2==0)) {
if (check(a[i],a[j])) {
add_edge(i,j,INF);
}
}
}
ans-=dinic(0,n+1);
printf("%lld\n", ans);
return 0;
}