Description
There is a triangular plank, vertical upright, with n (n+1)/2 nails on it, and (n+1) a lattice (when n=5 1). The distance between each nail and the surrounding nail is equal to D, and the width of each lattice is equal to D, and each lattice in addition to the leftmost and most right side of the grid is facing the bottom row of nails.
Let a small ball center with a diameter of slightly less than D is on the top of the nail on the board free roll, the ball every hit a nail may fall to the left or the right (1/2 probability each), and the center of the ball will be facing the next nail will be met. Example 2 is a possible path for the ball.
We know the probability of a small ball falling in the pi=pi=, where I is the number of the lattice, from left to right, 0,1,..., N.
The problem now is to calculate the probability of the ball falling in a lattice numbered m after unplugging certain nails. Suppose the bottom row of nails will not be pulled out. Example 3 is a possible path of a small ball after some nails are pulled out.
Input
The 1th behavior is integer n (2 <= n <= 50) and m (0 <= m <= N). The following n lines are sequentially the information from top to bottom n rows of nails, and each line ' * ' indicates that the nail is still there, '. ' Indicates that the nail is removed, note that the space character may appear anywhere in this n line.
Output
Only one row, which is an approximate fraction (0 written in 0/1), is the approximate PM of a small ball falling in a lattice numbered m. Definition of both the fraction: A/b is both an approximate fraction when and only if A and B are positive integers and a and b do not have a public factor greater than 1.
Sample Input
5 *. * * * * . * ** * * * *
Sample Output
7/16
In order to avoid the calculation of decimals, we assume that there are 2^n on the top of the ball, and then find the last to the bottom of a few balls, and then this number and 2^n to find the probability of GCD
#include <iostream> #include <stdio.h> #include <string.h> #include <stack> #include <queue > #include <map> #include <set> #include <vector> #include <math.h> #include <algorithm >using namespace std, #define LS 2*i#define rs 2*i+1#define up (i,x,y) for (i=x;i<=y;i++) #define DOWN (i,x,y) for (i=x; i>=y;i--) #define MEM (a,x) memset (A,x,sizeof (a)) #define W (a) while (a) #define LL long longconst double pi = acos (-1.0); # Define Len 63#define mod 19999997const int INF = 0x3f3f3f3f; LL Dp[55][55],a[100005],len;char str[10000]; ll GCD (ll A,ll b) {if (b==0) return A; Return GCD (b,a%b);} int main () {LL i,j,k,n,m; W (~scanf ("%i64d%i64d", &n,&m)) {len = 1; Up (I,1,n) {up (j,1,i) {scanf ("%s", str); if (str[0]== ' * ') a[len++] = 1; else a[len++] = 0; }} mem (dp,0); DP[1][1] = 1ll<≪n; Up (i,1,n) {LL t = (i-1) *i/2;//this row rightmost up (j,1,i)//Enumerate this disk all locations {if (a[j +T]//nails exist, then go left to the right of the general {Dp[i+1][j] + = DP[I][J]/2; Dp[i+1][j+1] + = DP[I][J]/2; } else//nail not, go down, because it is the shape of the triangle, so J to +1 dp[i+2][j+1] + = dp[i][j]; }} LL x = 1ll<<n; LL r = GCD (X,dp[n+1][m+1]); printf ("%i64d/%i64d\n", dp[n+1][m+1]/r,x/r); } return 0;}
POJ1189: Nails and Pellets (DP)