Fisa Flood (UVA 13052) by UKUNICHIA
Contest: 3738    RunID: 20174001    Status: Accepted    Date: Sat Oct 14 14:58:31 JST 2017


#include<bits/stdc++.h>
using namespace std;
using Int = long long;
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  int T;
  cin>>T;
  const int MAX=1555;
  static double dp[MAX][MAX];
  for(int i=0;i<MAX;i++)
    for(int j=0;j<MAX;j++)
      dp[i][j]=0;
  for(int t=1;t<=T;t++){
    cout<<"Case "<<t<<": ";
    int sum;
    {
      int a,b;
      cin>>a>>b;
      dp[a][b]=1.0;
      sum=a+b;
    }
    for(int i=sum;i>1;i--){
      for(int j=0;j<=i;j++){
	if(j>=MAX||i-j>=MAX||dp[j][i-j]==0) continue;
	//cout<<i<<" "<<j<<endl;
	for(int a=0;a<2;a++){
	  for(int b=0;b<2;b++){
	    int x=j,y=i-j;
	    double p=1.0;
	    if(a){
	      if(!x) continue;
	      p*=((double)x)/(x+y);
	      x--;
	    }else{
	      if(!y) continue;
	      p*=((double)y)/(x+y);
	      y--;
	    }
	    if(b){
	      if(!x) continue;
	      p*=((double)x)/(x+y);
	      x--;
	    }else{
	      if(!y) continue;
	      p*=((double)y)/(x+y);
	      y--;
	    }
	    if(a^b) dp[x][y+1]+=dp[j][i-j]*p;
	    else    dp[x+1][y]+=dp[j][i-j]*p;
	  }
	}
	dp[j][i-j]=0;
      }
    }

    cout<<fixed<<setprecision(3)<<dp[1][0]<<" "<<dp[0][1]<<endl;
    dp[1][0]=dp[0][1]=0;
  }

  return 0;
}