BZOJ2661: [BeiJing wc2012]連連看

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   資料   

2661: [BeiJing wc2012]連連看Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 483  Solved: 200
[Submit][Status]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 Output2 34
HINT

 

對於30%的資料,1<=a,b<=100

對於100%的資料,1<=a,b<=1000

 

Source

題解:

好吧,最大費用最大流還是老老實實把費用取負吧。。。

各種不理解?怎麼會是二分圖呢?怎麼這樣簡單粗暴就可以了?233

代碼:

  1 const inf=maxlongint;  2 type node=record  3      from,go,next,v,c:longint;  4      end;  5 var e:array[0..2000000] of node;  6     pre,head,q,d,c1,c2:array[0..1000000] of longint;  7     v:array[0..1000000] of boolean;  8     i,j,n,s,t,l,r,mincost,tot,x,y,z,a,b,maxflow:longint;  9 function min(x,y:longint):longint; 10  begin 11  if x<y then exit(x) else exit(y); 12  end; 13 procedure ins(x,y,z,w:longint); 14  begin 15  inc(tot); 16  with e[tot] do 17   begin 18   from:=x;go:=y;v:=z;c:=w;next:=head[x];head[x]:=tot; 19   end; 20  end; 21 procedure insert(x,y,z,w:longint); 22  begin 23  ins(x,y,z,w);ins(y,x,0,-w); 24  end; 25 function spfa:boolean; 26  var i,x,y:longint; 27  begin 28  fillchar(v,sizeof(v),false); 29  for i:=s to t do d[i]:=inf; 30  l:=0;r:=1;q[1]:=s;d[s]:=0;v[s]:=true; 31  while l<r do 32   begin 33   inc(l);if l=1000000 then l:=0; 34   x:=q[l];v[x]:=false; 35   i:=head[x]; 36   while i<>0 do 37    begin 38    y:=e[i].go; 39    if (e[i].v<>0) and (d[x]+e[i].c<d[y]) then 40     begin 41     d[y]:=d[x]+e[i].c; 42     pre[y]:=i; 43     if not(v[y]) then 44      begin 45      v[y]:=true; 46      inc(r);if r=1000000 then r:=0; 47      q[r]:=y; 48      end; 49     end; 50    i:=e[i].next; 51   end; 52  end; 53  exit(d[t]<>inf); 54  end; 55 procedure mcf; 56  var i,tmp:longint; 57  begin 58  mincost:=0;maxflow:=0; 59  while spfa do 60   begin 61   tmp:=inf; 62   i:=pre[t]; 63   while i<>0 do 64    begin 65    tmp:=min(tmp,e[i].v); 66    i:=pre[e[i].from]; 67    end; 68   inc(mincost,tmp*d[t]); 69   inc(maxflow,tmp); 70   i:=pre[t]; 71   while i<>0 do 72    begin 73    dec(e[i].v,tmp); 74    inc(e[i xor 1].v,tmp); 75    i:=pre[e[i].from]; 76    end; 77   end; 78  end; 79 function gcd(x,y:longint):longint; 80  begin 81  if y=0 then exit(x) else exit(gcd(y,x mod y)); 82  end; 83  84 procedure init; 85  begin 86  tot:=1; 87  readln(a,b); 88  s:=0;t:=10000; 89  for i:=a to b do insert(s,i,1,0); 90  for i:=a to b do insert(i+b,t,1,0); 91  for i:=a to b do 92   for j:=a to b do 93    if (trunc(sqrt(abs(i*i-j*j)))=sqrt(abs(i*i-j*j))) and (i<>j) then 94     if gcd(trunc(sqrt(abs(i*i-j*j))),min(i,j))=1 then 95      insert(i,b+j,1,-i-j); 96  end; 97 procedure main; 98  begin 99  mincost:=0;100  mcf;101  writeln(maxflow>>1,‘ ‘,-mincost>>1);102  end;103 begin104  assign(input,‘input.txt‘);assign(output,‘output.txt‘);105  reset(input);rewrite(output);106  init;107  main;108  close(input);close(output);109 end.  
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.