1 is not a prime number! On Mon, Oct 31, 2011 at 4:07 PM, mc2 . <qlearn...@gmail.com> wrote:
> Hey guys , > I am trying to solve this DP problem : > http://community.topcoder.com/stat?c=problem_statement&pm=10033&rd=13513 > SRM 422, DIV 2 ,level 2. > > Here is my DP solution. But it is not working. Can someone please tell me > the flaw in this code : > > #include"topheader.h" > > int prime(int i) > { > return i==1 ||i==2 || i==3 || i==5 || i==7 || i==11 || i==13|| i==17; > } > > int method(double p,double q) > { > double a[19][19]={0.,0.},b[19][19]={0.,0.}; > a[0][0]=1; > for(int i=1;i<=18;i++) > { > a[i][i]=pow(p/100,i); > b[i][i]=pow(q/100,i); > for(int j=1;j<=18;j++) > { > a[i][j]=(100-p)/100 * a[i-1][j] + (p/100) * a[i][j-1]; > b[i][j]=(100-q)/100 * b[i-1][j] + (q/100) * b[i][j-1]; > } > } > > > double ar[19]={0.}; > for (int i=0; i<18; i++) { > for (int j=18; j>=0; j--) > ar[j] = ((ar[j-1])*p + ar[j]*(100-p))/100.0; > } > > for(int i=0;i<=18;i++) > cout<<ar[i]<<"\t"<<a[i][18]<<"\n"; > cout<<"\n"; > > > > > > for(int i=1;i<19;i++) > { > double temp=0.0,temp1=0.0; > int k; > for(k=i;k<19;k++) > { > temp+=a[i][k]; > temp1+=b[i][k]; > } > a[i][k-1]=temp; > b[i][k-1]=temp1; > //cout<<temp<<" "<<temp1<<" "<<i<<" "<<k-1<<"\n"; > } > double res=0.0; > for(int i=1;i<19;i++) > { > for(int j=1;j<19;j++) > { > if(prime(i) || prime(j)) > { > res +=a[i][18] * b[i][18]; > // cout<<i<<" "<<j<<" "<<res<<"\n"; > } > } > } > cout<<res<<"\n"; > > return 0; > } > > int main() > { > double p,q; > cin>>p>>q; > method(p,q); > return 0; > } > > thanx in advance. > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to algogeeks@googlegroups.com. > To unsubscribe from this group, send email to > algogeeks+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.