Codevs 1709 Nails and pellets

Source: Internet
Author: User
Tags greatest common divisor

1709 Nails and Pellets 1999 NOI national competition
Time limit: 2 s
Space limit: 128000 KB
Topic rank: Master Master
Exercises
View Run Results
Title Description 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=, 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.


Enter a description input Description
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.

Outputs description Output Description
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 to sample
5 2


*

* .

* * *

* . * *

* * * * *

Sample output Sample Outputs
7/16

Data Size & Hint
Category tags tags click here to expand

Exercises

F[I][J] Indicates the number of balls falling to the first level of column J.

If the (i,j) position is ' * ', it falls to the left and right of the next layer in the same probability,

F[I+1][J]+=F[I][J],F[I+1][J+1]+=F[I][J];

If the (i,j) position is '. ' , it falls directly down to the lower level, so that he should have landed in 4 positions to the lower level,

f[i+2][j+1]+= (F[I][J]*4);

The total number of balls falling into the void Q=f[n+1][j] (j from 1 to n+1)

Finally the m+1 (because the topic m is starting from 0 number of M-bit, so +1) position the number of balls and the total number of balls Q about differentiation Jane gets the answer.

#include <cstdio> #include <iostream>using namespace Std;long long F[60][60],k,q;char s[60][60];int n,m; Long Long gcd (long long X,long long y)//Toss and divide method, ask for greatest common divisor {if (x%y==0) return y;else gcd (y,x%y);} int main () {cin>>n>>m;f[1][1]=1;for (int i=1;i<=n;i++) for  (int j=1;j<=i;j++)    {      cin >>s[i][j];      if (s[i][j]== ' * ')      {        f[i+1][j]+=f[i][j];        F[I+1][J+1]+=F[I][J];      }      else if (s[i][j]== '. ')        f[i+2][j+1]+= (f[i][j]*4);    } for (int i=1;i<=n+1;i++)   k+=f[n+1][i]; Q=GCD (f[n+1][m+1],k); cout<<f[n+1][m+1]/q<< "/" <<k /q;}

  

Attach a copy of the Wng

Based on probability solution

F (i,j) indicates the probability of falling on the position of row J of section I

If the (i,j) position is ' * ', f (i+1,j) +=f (i,j)/2; F (i+1,j+1) +=f (i,j)/2;

If the (i,j) position is '. ', f (i+2,j+1) +=f (I,J);

Initial value: F (=1/1)

F (n+1,m+1) is the request.

  

Codevs 1709 Nails and pellets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.