"BZOJ-3308" September coffee shop Maximum cost max Flow + linear sieve prime

Source: Internet
Author: User

3308: September Coffee shop Time limit:30 Sec Memory limit:128 MB
submit:159 solved:56
[Submit] [Status] [Discuss] Description

Deep painted in nine copies of a coffee let, how to deploy the coffee people the first thing she does every day we assume that she has n raw materials, the first ingredient number I, the provision of a cup of coffee will need to be here several kinds of together. However, some raw materials can not be in one Cup at the same time, if the two number of raw materials i,j, when and only if I and J coprime, can be in the same cup. Now you want to know, if you use these n raw materials to adjust the same cup of coffee, the use of the number of raw materials and the maximum amount.

Input

A number n

Output

Title

Sample Input10
Sample Output30
HINT

1<=n<=200000

SourceSolution

From the range of N, it is difficult to see directly is the network flow, but careful analysis, still can think of

The more powerful the map, the first two conclusions are needed:

1. A number of qualitative factors, up to two

2. And these two qualitative factors, one less than $\sqrt{n}$, a greater than $\sqrt{n}$

Then consider the number of 1~n to be screened out.

Consider building a two-part map

Prime number with a source less than $\sqrt{n}$, with a capacity of 1 and a cost of 0

Prime number greater than $\sqrt{n}$ to connect edge, capacity 1, cost 0

Prime number less than $\sqrt{n}$ $a$ to a prime number greater than $\sqrt{n}$ $b$ edge with a capacity of 1 and a cost of $v_{ab}-v_{a} -v_{b}$

$V _{a}$ represents the maximum benefit of a single selected $a$ $v_{a}=a^{lgn/lga}$

$V _{b}$ represents the maximum benefit of selecting $b$ alone $v_{b}=b$

$V _{ab}$ represents the maximum benefit of simultaneous $a$ and $b$ $v_{ab}=a^{lg (n/b)/lga}*b$

This efficiency is not particularly high, then you can add two optimizations:

1. If a prime number >N/2, then it can only be selected separately, without the edge

2. If the cost of an edge is <0, it can be completely non-connected

Then consider the maximum cost maximum flow can be

Code
#include <iostream>#include<cstdio>#include<cmath>#include<algorithm>#include<cstring>#include<queue>using namespacestd; inlineintRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1;CharCh=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;} #defineMAXN 200010#defineMAXM 200000*10+10#defineINF 0x7fffffffintN,ans;structedgenode{intNext,to, from, Cap,cost;} EDGE[MAXM]; intHead[maxn],cnt=1; //InitInlinevoidAddintUintVintWintc) {cnt++; Edge[cnt].next=head[u]; head[u]=CNT; EDGE[CNT]. from=u; Edge[cnt].to=v; Edge[cnt].cap=w; edge[cnt].cost=C;} InlinevoidInsertintUintVintWintc) {Add (u,v,w,c); Add (V,u,0,-c);} //AddedgeBOOLMARK[MAXN];intdis[maxn],s,t; inlineBOOLSPFA () {Queue<int>q; memset (Mark,0,sizeof(Mark));  for(intI=s; i<=t; i++) dis[i]=-inf; Q.push (S); Mark[s]=1; dis[s]=0;  while(!Q.empty ()) {             intnow=Q.front (); Q.pop ();  for(intI=head[now]; I I=edge[i].next)if(Edge[i].cap && dis[edge[i].to]<dis[now]+edge[i].cost) {Dis[edge[i].to]=dis[now]+Edge[i].cost; if(!mark[edge[i].to]) mark[edge[i].to]=1, Q.push (edge[i].to); } Mark[now]=0; }     returnDis[t]>0; } InlineintDfsintLocintLow ) {Mark[loc]=1; if(loc==t)returnLow ; intW,used=0;  for(intI=head[loc]; I I=edge[i].next)if(Edge[i].cap && dis[edge[i].to]==dis[loc]+edge[i].cost &&!)Mark[edge[i].to]) {W=dfs (Edge[i].to,min (low-used,edge[i].cap)); Edge[i].cap-=w; edge[i^1].cap+=w; used+=W; Ans+=w*edge[i].cost;if(Used==low)returnLow ; }     returnused;} Inlinevoidzkw () {inttmp=0;  while(SPFA ()) {mark[t]=1;  while(Mark[t]) memset (Mark,0,sizeof(Mark)), tmp+=DFS (S,inf); } } //MaxflowmaxcostBOOLFLAG[MAXN];intPrime[maxn],tot; inlinevoidPrework (intMaxx) {flag[1]=1;  for(intI=2; i<=maxx; i++)         {             if(!flag[i]) prime[++tot]=i;  for(intj=1; J<=tot && i*prime[j]<=maxx; J + +) {Flag[i*prime[j]]=1; if(! (I%prime[j])) Break; }         } } //Get PrimeInlineintCalcintNintx) {Long Longt=x;  while(t*x<=n) t=t*x; returnT;} //CalcInlinevoidMake () {S=0, t=tot+1;intpos=0;  for(intI=1; i<=tot; i++)         {             if(prime[i]>=n/2) {Ans+=prime[i];Continue;} if((Long Long) prime[i]*prime[i]<=N) Insert (S,i,1,0), ans+=Calc (n,prime[i]); ElsePOS= (!pos)? I:pos,insert (I,t,1,0), ans+=Prime[i]; }      for(intI=1; i<pos; i++)          for(intJ=pos; j<=tot; J + +)             {                 if((Long Long) prime[i]*prime[j]>n) Break; intTmp=calc (N/prime[j],prime[i]) *prime[j]-calc (N,prime[i])-Prime[j]; if(tmp>0) Insert (I,j,1, TMP); } } //BuildintMain () {n=read ();     Prework (n);     Make ();     ZKW (); printf ("%d\n", ans+1); return 0; }

Ignore the inline of the wild heart. && own yy maximum cost max flow ... It seems to be possible?

"BZOJ-3308" September coffee shop Maximum cost max Flow + linear sieve prime

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.