Hihocoder #1075: Unlocking Magic III (combinatorial math + dynamic Programming)

Source: Internet
Author: User

Topic links

The main topic: There are N boxes, each box with a box of keys, you can use magic to open the K-box, ask the final can open all the box probability is how much.

Idea: First of all we think that if a set of boxes of open targets can constitute a ring, then the box as long as the open one is arbitrary. Probability, the best thing to do is to use the number of occurrences of the A event/event totals. The total number of events is very good, is to choose K Open in n boxes: c[n][k].

Then consider how many kinds of legal open way, I began to think is directly with the combination of mathematical chaos, multiplication principle, set a total of tot a ring, each ring size is a1,a2,a3...atot; calculate the time first A1*a2*...*atot, then if tot<k, and then multiply on C N [K-tot]; it feels like it's right, but Jiang (FAEBDC) says I'm wrong about it.

Think again, find that there will be a lot of repetition, the key is the final C[n][k-tot] too much uncertainty, actually look at the data range (n<=300), if you want to really directly multiply to get the answer, it can completely increase the data range to 10000. We consider the statistical process details, remember F[i][j] said the first I ring opened a J box to ensure that each ring open at least one of the scheme number, then as long as the f[i][j]!=0, you can move backwards, enumerate the i+1 ring open a few boxes to remember as AD, then F[i+1][j+ad]+=f[i] [J]*c[sz[i+1]][k], that is, the number of options for ad in the i+1 ring * The number of scenarios for the first I ring (here is a step count, to use the multiplication principle) and (this is the classification count, with ad discussion, using the addition principle).

The program does not know how, on the Hiho, and the nominal to pat a bit wrong also did not. The great God who passed by helps to look at it. Thank you, sir.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 #defineN 3107 intN,k,to[n],tot,sz[n],t;BOOLVisit[n];8 DoubleF[n][n],c[n][n];9 voidInit ()Ten{c[1][0]=1.0, c[1][1]=1.0; One      for(intI=2; i<= -; i++) A{c[i][0]=1.0;  -          for(intj=1; j<=i;j++) c[i][j]=c[i-1][j-1]+c[i-1][j];  -     } the } - intMain () -{Init ();cin>>T; -      while(t--){ +scanf"%d%d", &n,&k); for(intI=1; i<=n;i++) scanf ("%d",&To[i]);  -memset (SZ,0,sizeof(SZ)); memset (Visit,0,sizeof(visit)); +      for(intI=1; i<=n;i++) A     if(!Visit[i]) at{intCnt=0, Cur=i; for(;! visit[cur];visit[cur]=1, cnt++,cur=To[cur]);  -sz[++tot]=CNT; -     } -     if(K<tot) {printf ("%.9lf\n",0.0);Continue;} -Memset (F,0,sizeof(f)); f[0][0]=1.0; -      for(intI=0; i<tot;i++) in          for(intj=0; j<k;j++)  -              if(F[i][j]) to{ for(intAd=1; ad<=sz[i+1]&&ad+j<=k;ad++) +f[i+1][j+ad]+=f[i][j]*c[sz[i+1]][ad]; -              } theprintf"%.9lf\n", f[tot][k]/c[n][k]); *     } $     return 0; Panax Notoginseng } -     
WA Code

Send all the things that make up the data and the camera.

1#include <iostream>2#include <ctime>3#include <cstdio>4#include <algorithm>5 using namespacestd;6 BOOLvisit[100000];7 intMain ()8{Freopen ("data.in","W", stdout); 9Srand (Time (0));Tencout<<1<<Endl; One     intn= -;  Acout<<n<<endl;intsum=0; -      while(sum<N) -{intA=rand ();  thea=a%n+1;  -         if(visit[a]==0) -{sum++; -visit[a]=1; +cout<<a<<' '; -         } +     } A     return 0; at } -              -         
Make Data
1#include <iostream>2#include <cstdio>3#include <algorithm>4#include <cstdlib>5 using namespacestd;6 intMain ()7{ for(intI=1; i<= +; i++) 8{System ("Hiho1075data"); 9System"hiho1075 < data.in >a.out");TenSystem"hiho1075r < data.in >b.out");  OneSystem"FC A.out B.out"); A     } -     return 0; -}
Duipai

AC-Capable (transfer from http://www.cnblogs.com/wzj-is-a-juruo/p/4802655.html)

1#include <cstdio>2#include <cctype>3#include <queue>4#include <cmath>5#include <cstring>6#include <algorithm>7 #defineRep (i,s,t) for (int i=s;i<=t;i++)8 #defineDwn (i,s,t) for (int i=s;i>=t;i--)9 #defineren for (int i=first[x];i;i=next[i])Ten using namespacestd; OneInlineintRead () { A     intx=0, f=1;CharC=GetChar (); -      for(;! IsDigit (c); C=getchar ())if(c=='-') f=-1; -      for(; IsDigit (c); C=getchar ()) x=x*Ten+c-'0'; the     returnx*F; - } - Const intmaxn=310; - intN,K,SIZE[MAXN],CNT,V[MAXN],VIS[MAXN]; + DoubleF[MAXN][MAXN],C[MAXN][MAXN]; - intMain () { +     intt=read (); Ac[0][0]=1; atRep (I,0, -) Rep (J,0, i) c[i+1][j+1]+=c[i][j],c[i+1][j]+=C[i][j]; -      while(t--) { -N=read (); K=read (); cnt=0; -memset (Size,0,sizeof(size)); -memset (Vis,0,sizeof(Vis)); -Rep (I,1, N) v[i]=read (); inRep (I,1, N)if(!Vis[i]) { -cnt++;intj=i; to              Dosize[cnt]++,vis[j]=1, J=v[j]; while(j!=i); +         } -Memset (F,0,sizeof(f)); thef[1][0]=1.0;intCur=0; *Rep (I,1, CNT) { $Rep (J,0, cur) rep (k0,1, Size[i]) f[i+1][j+k0]+=f[i][j]*C[size[i]][k0];Panax Notoginsengcur+=Size[i]; -         } theprintf"%.6lf\n", f[cnt+1][k]/c[n][k]); +     } A     return 0; the}
AC Code

Hihocoder #1075: Unlocking Magic III (combinatorial math + dynamic Programming)

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.