1. 程式人生 > >BZOJ2661: [BeiJing wc2012]連連看

BZOJ2661: [BeiJing wc2012]連連看

node jin pre 手動 三個點 一段 lib 整數 最大流

Description

凡是考智商的題裏面總會有這麽一種消除遊戲。不過現在面對的這關連連看可不是QQ遊戲裏那種考眼力的遊戲。我們的規則是,給出一個閉區間[a,b]中的全部整數,如果其中某兩個數x,y(設x>y)的平方差x2-y2是一個完全平方數z2,並且y與z互質,那麽就可以將x和y連起來並且將它們一起消除,同時得到x+y點分數。那麽過關的要求就是,消除的數對盡可能多的前提下,得到足夠的分數。快動手動筆算一算吧。

Input


只有一行,兩個整數,分別表示a,b。

Output

兩個數,可以消去的對數,及在此基礎上能得到的最大分數。

Sample Input

1 15

Sample Output

2 34

HINT

對於30%的數據,1<=a,b<=100

對於100%的數據,1<=a,b<=1000

題目傳送門 炒雞大水題,做題+看題一共20分鐘 看到第一句我還以為是DP,然後猛打臉 然後想該怎麽做。。。1<=a,b<=1000? 最大費用最大流???
好像是對的!,那麽我把一個點拆成三個點 ST 連 X1 流量為1 費用為零 X1 連能夠消除的Y2 流量為1 費用為X+Y Y2 連Y3 流量為1 費用為零 然後Y3 連ED 流量為1 費用為零 然後bzoj崩了一段時間
代碼如下:
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring> 
#include<algorithm>
using namespace std;
struct node{
    int x,y,c,d,next,other;
}a[2100000];int len,last[2100000];
void ins(int x,int y,int c,int d)
{
    int k1,k2;
    k1=++len;
    a[len].x
=x;a[len].y=y;a[len].c=c;a[len].d=d; a[len].next=last[x];last[x]=len; k2=++len; a[len].x=y;a[len].y=x;a[len].c=0;a[len].d=-d; a[len].next=last[y];last[y]=len; a[k1].other=k2; a[k2].other=k1; } int d[4100],list[4100],pre[4100],cc[4100],st,ed,head,tail,t[110][110]; bool v[4100]; int ans,sum; bool spfa() { for(int i=1;i<=ed;i++)d[i]=-9999999; d[st]=0; memset(v,false,sizeof(v));v[st]=true; memset(cc,0,sizeof(cc));cc[st]=999999999; list[1]=st;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&&d[y]<d[x]+a[k].d) { d[y]=d[x]+a[k].d; pre[y]=k; cc[y]=min(cc[x],a[k].c); if(!v[y]) { v[y]=true; list[tail++]=y; if(tail==ed+1)tail=1; } } } head++;if(head==ed+1)head=1; v[x]=false; } if(d[ed]==-9999999)return false; ans+=d[ed]*cc[ed];sum+=cc[ed]; int x=ed; while(x!=0) { int k=pre[x]; a[k].c-=cc[ed];a[a[k].other].c+=cc[ed]; x=a[k].x; } return true; } int gcd(int a,int b) { if(a==0)return b; return gcd(b%a,a); } int l,r; int p[1100],cnt; int main() { scanf("%d%d",&l,&r);cnt=0; len=0;memset(last,0,sizeof(last)); for(int i=l;i<=r;i++)p[i]=++cnt; st=4*cnt+1,ed=st+1; for(int i=1;i<=cnt;i++)ins(st,i,1,0); for(int i=r;i>l;i--) { for(int j=i-1;j>=l;j--) { double k=sqrt(i*i-j*j); if(k==floor(k)&&gcd(j,(int)k)==1) ins(p[i],p[j]+cnt,1,i+j),ins(p[j],p[i]+cnt,1,i+j); } } for(int i=1;i<=cnt;i++)ins(cnt+i,cnt*2+i,1,0); for(int i=1;i<=cnt;i++)ins(cnt*2+i,ed,1,0); sum=ans=0; while(spfa()){} printf("%d %d\n",sum/2,ans/2); return 0; }

BZOJ2661: [BeiJing wc2012]連連看