"BZOJ-4514" digital pairing maximum cost Max Stream + mass factor decomposition + binary graph + greedy + linear sieve

Source: Internet
Author: User

4514: [Sdoi2016] Digital pairing time limit:10 Sec Memory limit:128 MB
submit:726 solved:309
[Submit] [Status] [Discuss] Description There are n kinds of numbers, the first number is AI, there are bi, the weight is CI. If two digital AI, AJ satisfies, AI is a multiple of AJ, and Ai/aj is a prime number, then these two numbers can be paired and gain CIXCJ value. A number can only participate in a single pairing and may not participate in pairing. The maximum number of pairs to be paired, provided that the sum of the values obtained is not less than 0. Input first line an integer n. The second row n integers a1, A2 、......、 an. The third row n integers b1, B2 、......、 bn. row n integers c1, C2 、......、 CN. Output

Number of rows, maximum number of pairs

Sample Input3
2 4 8
2 200 7
-1-2 1Sample Output4HINT

N≤200,ai≤10^9,bi≤10^5,∣ci∣≤10^5

Source

Acknowledgement Menci Upload

Solution

Two-part map to build the pattern, analysis

If $a[i]$ and $a[j]$ coprime, it may be useful to decompose them into the mass factor, coprime that the quality factor is about to go after the quality factor coprime, or only one quality factor

So consider pre-treatment of linear sieve prime number, re-quality factor decomposition, containing odd numbers of qualitative factors and containing an even number of qualitative factors two rows

If the $a[i]$ and $a[j]$ can be connected ($a [i]$ is a mass factor of the $a[j]$ of the mass factor is even), then $i--->j$ capacity $inf$, cost $c[i]*c[j]$

By $s--->odd[i]$ with capacity $b[odd[i]]$, the cost is $0$; $even[i]--->t$ capacity $b[even[i]]$ cost $0$

And then run the maximum cost maximum flow, because the maximum cost of the maximum flow per augmentation path cost strictly does not decline, so, greedy to run to the total cost of $<=0$, alone judge can

Details that are prone to problems:

1. Note Open Longlong ...

The range of the 2.INF to be noted (habitual open 0x7fffffff leads to WA5 Group ...)

3. Non-violent screening and violence decomposition, time complexity is not scientific

Code
#include <iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<queue>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 1010#defineMAXM 100010intN,A[MAXN],B[MAXN];Long LongC[MAXN];BOOLf=0;structedgenode{intNext,cap,to, from;Long LongCost ;} EDGE[MAXM];intHead[maxn],cnt=1;voidAddintUintVintWLong Longc) {CNT++; Edge[cnt].to=V;EDGE[CNT]. from=u;edge[cnt].cap=w;edge[cnt].cost=c;edge[cnt].next=head[u];head[u]=CNT;}voidInsertintUintVintWLong Longc) {Add (u,v,w,c); Add (V,u,0,-c);}int  from[Maxn],s,t;Long LongDIS[MAXN];BOOLMARK[MAXN];#defineINF 10000000000000LLBOOLSPFA () {Queue<int>P;  for(intI=s; i<=t; i++) Dis[i]=-inf; Memset from,0,sizeof( from)); Q.push (S); Mark[s]=1; dis[s]=0;  while(!Q.empty ()) {            intNow=q.front (); Q.pop (); mark[now]=0;  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; from[edge[i].to]=i; if(!mark[edge[i].to]) Q.push (edge[i].to), mark[edge[i].to]=1; }        }     returndis[t]!=-inf;}Long Longcost;intFlow;voidMaxcostflow () {intflow=inf;  for(intI= from[T]; I I= from[Edge[i]. from]) Flow=min (flow,edge[i].cap); if((Long Long) cost+flow*dis[t]>=0LL) { for(intI= from[T]; I I= from[Edge[i]. from]) Edge[i].cap-=flow,edge[i^1].cap+=flow; Flow+=flow; cost+=dis[t]*flow; }    ElseFlow+=abs (Cost/dis[t]), cost-= (Cost/dis[t]) *dis[t],f=1; //Flow+=flow; //printf ("%d%i64d%i64d\n", flow,cost,dis[t]);}intprime[35001],tot;BOOLflag[35001]; voidprework () {flag[1]=1;  for(intI=2; i<=32000; i++)        {            if(!flag[i]) prime[++tot]=i;  for(intj=1; j<=tot&&i*prime[j]<=32000; J + +) Flag[i*prime[j]]=1; }}intOdd[maxn],even[maxn],ot,et;BOOLCheckintXinty) {    if(x%y && y%x | |!x | |!y)return 0; intTmp=max (x/y,y/x);  for(intI=1; i<=tot&&prime[i]<tmp; i++)        if(tmp%prime[i]==0)return 0; return 1;} voidBuild () {prework (); S=0, t=n+1;  for(intI=1, ct=0; i<=n; i++,ct=0)        {             for(intj=1, Tmp=a[i]; j<=tot; j++,tmp=A[i]) while(tmp%prime[j]==0) tmp/=prime[j],ct++; //else if (prime[j]>tmp) break;            if(ct&1) Odd[++ot]=i;Elseeven[++et]=i; }     for(intI=1; i<=ot; i++)         for(intj=1; j<=et; J + +)            if(check (a[odd[i]],a[even[j])) Insert (Odd[i],even[j],inf,c[odd[i]]*C[EVEN[J]])/*, printf ("%d-->%d:%d/%i64d\n", Odd[i],even[j],inf,c[odd[i]]*c[even[j]])*/;  for(intI=1; i<=ot; i++) Insert (S,ODD[I],B[ODD[I]],0LL)/*, printf ("%d-->%d:%d/%i64d\n", S,ODD[I],B[ODD[I]],0LL)*/;  for(intI=1; i<=et; i++) Insert (EVEN[I],T,B[EVEN[I]],0LL)/*, printf ("%d-->%d:%d/%i64d\n", EVEN[I],T,B[EVEN[I]],0LL)*/; //printf ("%d%d%d\n", ot,et,cnt);}intMain () {//freopen ("menci_pair.in", "R", stdin);//freopen ("Menci_pair.out", "w", stdout);n=read ();  for(intI=1; i<=n; i++) a[i]=read ();  for(intI=1; i<=n; i++) b[i]=read ();  for(intI=1; i<=n; i++) c[i]= (Long Long) read ();    Build ();  while(SPFA () &&!f) maxcostflow (); printf ("%d\n", Flow); return 0;}

At that time the only thought and the problem of the same solution, but finally did not know what the wrong place, the results even violent points are not .... (After coming back so long to remember to rewrite it again.)

"BZOJ-4514" digital pairing maximum cost Max Stream + mass factor decomposition + binary graph + greedy + linear sieve

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.