Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 8073 | Accepted: 2516 |
Description
讓一個直徑略小於d的小球中心正對着最上面的釘子在板上自由滾落,小球每碰到一個釘子都可能落向左邊或右邊(概率各1/2),且球的中心還會正對着下一顆將要碰上的釘子。例如圖2就是小球一條可能的路徑。
我們知道小球落在第i個格子中的概率pi=pi=,其中i爲格子的編號,從左至右依次爲0,1,...,n。
現在的問題是計算拔掉某些釘子後,小球落在編號爲m的格子中的概率pm。假定最下面一排釘子不會被拔掉。例如圖3是某些釘子被拔掉後小球一條可能的路徑。
Input
Output
Sample Input
5 2 * * . * * * * . * * * * * * *
Sample Output
7/16
Source
原題鏈接:http://poj.org/problem?id=1189
題目是中文就不多囉嗦了,但是有一點,如果某個點沒有釘子,那麼小球會落到下面第二層的位置。
理解以後就和POJ1136 The Triangle差不多了。
由於概率每次都要處於2,並且分數加法有點麻煩,所以,開始時一個數的值設爲2^n,
這樣便可簡化運算.
運算方法有兩種,
一.dp[i][j]+=(dp[i-1][j-1]+dp[i-1][j])/2;
二.dp[i+1][j]+=dp[i][j]/2,dp[i+1][j+1]+=dp[i][j]/2;
相對來說第二種方法更簡單一點.
樣例過程:
32
16 0
8 8 0
4 0 20 0
2 2 10 10 0
1 2 14 10 5 0
AC代碼:
/**
* 行有餘力,則來刷題!
* 博客鏈接:http://blog.csdn.net/hurmishine
*
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long LL;
const int maxn=50+5;
char a[maxn][maxn];
LL dp[maxn][maxn];
int n,m;
LL GCD(LL x,LL y)
{
if(y==0)
return x;
return GCD(y,x%y);
}
int main()
{
//freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
while(cin>>n>>m)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
cin>>a[i][j];
}
memset(dp,0,sizeof(dp));
LL maxx=(LL)1<<n;
dp[0][0]=maxx;
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
if(a[i][j]=='*')
{
dp[i+1][j]+=dp[i][j]/2;
dp[i+1][j+1]+=dp[i][j]/2;
}
else
{
dp[i+2][j+1]+=dp[i][j];
//dp[i][j]=0;
}
}
}
/**
for(int i=0;i<=n;i++)
{
for(int j=0;j<=i;j++)
cout<<dp[i][j]<<" ";
cout<<endl;
}
*/
//cout<<dp[n][m]<<endl;
LL gcd=GCD(maxx,dp[n][m]);
cout<<dp[n][m]/gcd<<"/"<<maxx/gcd<<endl;
}
return 0;
}