BZOJ3275: Number

【传送门:BZOJ3275


简要题意:

  给出n个数,要求选出一部分数,这一部分数两两之间(a,b)必须不能同时满足下面的要求:

  1.a2+b2=c2(c为正整数)

  2.gcd(a,b)=1

  求出选出的数的最大和


题解:

  与BZOJ3158思路相同

  不过价值为每个数


参考代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<cmath>
using namespace std;
typedef long long LL;
struct node
{
    int x,y,next,other;LL c;
}a[2100000];int len,last[3100];
void ins(int x,int y,LL c)
{
    int k1=++len,k2=++len;
    a[k1].x=x;a[k1].y=y;a[k1].c=c;
    a[k1].next=last[x];last[x]=k1;
    a[k2].x=y;a[k2].y=x;a[k2].c=0;
    a[k2].next=last[y];last[y]=k2;
    a[k1].other=k2;
    a[k2].other=k1;
}
int h[3100],list[3100],st,ed;
bool bt_h()
{
    memset(h,0,sizeof(h));h[st]=1;
    list[1]=st;
    int head=1,tail=2;
    while(head!=tail)
    {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next)
        {
            int y=a[k].y;
            if(a[k].c>0&&h[y]==0)
            {
                h[y]=h[x]+1;
                list[tail++]=y;
            }
        }
        head++;
    }
    if(h[ed]==0) return false;
    else return true;
}
LL findflow(int x,LL f)
{
    if(x==ed) return f;
    int s=0,t;
    for(int k=last[x];k;k=a[k].next)
    {
        int y=a[k].y;
        if(a[k].c>0&&h[y]==(h[x]+1)&&f>s)
        {
            t=findflow(y,min(a[k].c,f-s));
            s+=t;
            a[k].c-=t;a[a[k].other].c+=t;
        }
    }
    if(s==0) h[x]=0;
    return s;
}
LL gcd(LL a,LL b)
{
    if(a==0) return b;
    else return gcd(b%a,a);
}
LL A[3100],B[3100];
bool check(LL x,LL y)
{
    LL c=sqrt(x*x+y*y);
    if(c*c!=x*x+y*y) return false;
    if(gcd(x,y)>1) return false;
    return true;
}
int main()
{
    int n;
    scanf("%d",&n);
    st=0;ed=n+1;
    len=0;memset(last,0,sizeof(last));
    LL sum=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&A[i]);
        sum+=A[i];
        if(A[i]%2==0) ins(st,i,A[i]);
        else ins(i,ed,A[i]);
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(check(A[i],A[j])==true&&(A[i]%2==0)&&(A[j]%2==1))
            {
                ins(i,j,999999999);
            }
        }
    }
    while(bt_h()==true) sum-=findflow(st,999999999);
    printf("%lld\n",sum);
    return 0;
}

 

posted @ 2018-03-27 20:40  Star_Feel  阅读(158)  评论(0编辑  收藏  举报