508. Black-white balls
1 #include <iostream> 2 #include <string.h> 3 #include <string> 4 #include <fstream> 5 #include <algorithm> 6 #include <stdio.h> 7 #include <vector> 8 #include <queue> 9 #include <set> 10 #include <cmath> 11 using namespace std; 12 const double eps = 1e-8; 13 const double pi=acos(-1.0); 14 const int INF=0x7fffffff; 15 unsigned long long uINF = ~0LL; 16 #define MAXN 1007 17 typedef long long LL; 18 LL c[51][51]; 19 void init() 20 { 21 memset(c,0,sizeof(c)); 22 c[0][0]=1; 23 for(LL i=1;i<=50;i++) 24 {c[i][0]=1; 25 for(LL j=1;j<=i;j++) 26 c[i][j]=c[i-1][j]+c[i-1][j-1]; 27 } 28 29 } 30 int n,l1,l2,p,l; 31 bool check(int s,int e) 32 { 33 int k;double P=0,per=(double)p/100; 34 for(k=s;k<=e;k++) 35 { 36 double temp=(double)c[k][l1]/(double)c[n][l]*(double)c[n-k][l2]; 37 P+=temp/(double)(n+1)*(double)(l+1); 38 39 //cout<<P<<' '<<per<<endl; 40 if(P>per)return true; 41 else if(fabs(P-per)<eps)return true; 42 } 43 return false; 44 } 45 int main() 46 { 47 init(); 48 49 while(scanf("%d%d%d%d",&n,&l1,&l2,&p)!=EOF) 50 { 51 l=l1+l2; 52 int len=INF; 53 int ans1=l1,ans2=n-l2; 54 if(p!=100) 55 for(int i=0;i<=n;i++) 56 for(int j=i;j<=n;j++) 57 { 58 if(j-i+1>=len)continue; 59 if(check(i,j)) 60 { 61 ans1=i;ans2=j;len=j-i+1; 62 } 63 } 64 printf("%d %d\n",ans1,ans2); 65 } 66 67 return 0; 68 }
条件概率公式,搜索区间