bzoj3275 Number 最小割

17 篇文章 0 订阅

Description


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

Solution


带权最大独立集,考虑转化成二分图用最小割解决

注意到我们可以根据奇偶性黑白染色,即奇数和奇数不满足条件一,偶数和偶数不满足条件二,这两个集合内互不影响

这里还是证明一下性质一吧,两奇数的平方和可以这么表示

(2n+1)2+(2m+1)2=c2 ( 2 n + 1 ) 2 + ( 2 m + 1 ) 2 = c 2

拆开
4(m2+n2+m+n)+2=c2 4 ( m 2 + n 2 + m + n ) + 2 = c 2

显然有 2|c 2 | c ,我们令 c2=4k(kZ) c 2 = 4 k ( k ∈ Z ) ,那么有
(m2+n2+m+n)+12=k ( m 2 + n 2 + m + n ) + 1 2 = k

这是不成立的,然后就搞定了

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值