Bzoj2661[beijing wc2012] repeatedly look

Source: Internet
Author: User

Bzoj2661[beijing wc2012] repeatedly look

Test instructions

Give all the integers in a closed interval [a, b], if one of the two number x, Y (set x>y) squared difference x2-y2 is a complete square number Z2, and Y and z coprime, then you can eliminate the x and y together, and get the x+y point score. The maximum number of points to be eliminated on the basis of as many as possible.

Exercises

Each number is split into two points, s and left points with a flow rate of 1, an edge with a cost of 0, and a right-hand point and a T-link flow of 1, and a cost of 0 for the edge. If the i,j is valid, then simultaneously to the left I to the right J and the left J to the right I connected flow is 1, the cost is i+j edge. This problem is different from bzoj4514, because each number only appears once, so when the left I and the right J are eliminated, the left J and the right side I will be eliminated the next time, the other number can no longer be eliminated with them. and bzoj4514 each number has multiple, may not be exhausted, if the practice of this problem may appear another number of it has been eliminated part of the repeated elimination, resulting in the result is not exactly twice times the positive solution. So bzoj4514 can not be broken down, and this problem may be broken, the answer is the algorithm run out of the maximum flow and the maximum "cost" divided by 2.

Code:

1#include <cstdio>2#include <algorithm>3#include <cstring>4#include <cmath>5#include <queue>6 #defineInc (I,J,K) for (int i=j;i<=k;i++)7 #definell Long Long8 #defineMAXN 50009 #defineINF 0X3FFFFFFFTen using namespacestd; One  A structe{intF,t;intC,w;intn;}; E es[2000000];intESS,G[MAXN]; -InlinevoidPeintFintTintCintW) { -Es[++ess]= (e) {f,t,c,w,g[f]}; g[f]=ess; Es[++ess]= (e) {t,f,0,-w,g[t]}; g[t]=ess; the } - voidInit () {ess=-1; memset (g,-1,sizeof(g));} -Queue <int> q;intD[MAXN],COST,FLOW,FR[MAXN];BOOLINQ[MAXN],VIS[MAXN]; - BOOLSPFA (intSintt) { +      while(! Q.empty ()) Q.pop (); memset (Vis,0,sizeof(VIS)); memset (INQ,0,sizeof(INQ)); -Q.push (s); vis[s]=1; inq[s]=1; d[s]=0; +      while(!Q.empty ()) { A         intX=q.front (); Q.pop (); inq[x]=0; at          for(inti=g[x];i!=-1; i=es[i].n)if(es[i].c&& (!vis[es[i].t]| | d[es[i].t]<d[x]+ES[I].W)) { -vis[es[i].t]=1; D[ES[I].T]=D[X]+ES[I].W; fr[es[i].t]=i; -             if(!inq[es[i].t]) Q.push (es[i].t), inq[es[i].t]=1; -         } -     } -     if(!vis[t])return 0;Else return 1; in } - voidAdvancedintSintt) { to     intA=inf; for(intI=T;I!=S;I=ES[FR[I]].F) a=min (A,ES[FR[I]].C); flow+=A; +      for(intI=T;I!=S;I=ES[FR[I]].F) es[fr[i]].c-=a,es[fr[i]^1].c+=a,cost+=a*ES[FR[I]].W; - } the voidMaxflowmaxcost (intSintt) { *      while(SPFA (S,T)) advanced (s,t); $ }Panax Notoginseng intN,s,t,l,r; - intgcdintAintb) {returnb==0? A:GCD (b,a%b);} the BOOLCheckintXinty) { +     if(x<=y)return 0;DoubleZ=SQRT (X*x-y*y);if(Z!= (Double)((int) (z)))return 0; A     intZz= (int) Z;if(GCD (y,zz)! =1)return 0;return 1; the } + intMain () { -scanf"%d%d", &l,&r); n=l-1; s=0; t=2* (r-l+1)+1; $Init (); Inc (I,L,R) PE (S,I-N,1,0), PE (i-n+ (r-l+1), T,1,0); $Inc (I,L,R) Inc (J,L,R)if(check (I,J)) PE (i-n,j-n+ (r-l+1),1, i+j), PE (j-n,i-n+ (r-l+1),1, i+j); Maxflowmaxcost (s,t); -printf"%d%d",flow>>1,cost>>1); -     return 0; the}

20160424

Bzoj2661[beijing wc2012] repeatedly look

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.