Will the ball come out of the well or not? 
A small ball moves at a constant velocity $v$ along a horizontal surface and at point $A$ falls into a vertical well of depth $H$ and radius $r$. the velocity of the ball forms an angle $\theta$ with the diameter of the well drawn through point $A$. Determine the relation between $v,H,r,\theta$ for which ball can "get out" of the well after elastic impacts with walls (friction losses should be neglected)
  
The answer is $\dfrac{nr\cos\theta}{v}=k\sqrt{\frac{2H}{g}}$, where $n,k$ are integers and mutually prime numbers

My question is :
Since collisions are elastic, velocity while falling should get conserved and help the ball to come out and even there is no force changing velocity in horizontal  direction, so the ball should come out of the well/ditch without any mathematical condition, but this does not happen in my book--a particular condition is given.
I want PSE to tell me why is there a particular condition for ball to come out. Why can't the ball come out if it is thrown at any angle, at any velocity in the well?
EDIT 
I got a beautiful answer from you guys but still want to ask:
Why do we need a condition for the ball to come out of the well? The vertical component of velocity after the ball hits the ground of the well gets turned and that will bring the ball back up, and I don't see any kind of force stopping the ball from getting out of the well.
 A: 
Take a 2D case for simplicity. Suppose the ball entered the well at point A. It did hit at points R1, H1, H2 and R2 so lucky that exactly fit point B thus gone out. This happened only because ratio of diameter to depth of the well is so good that allowed the ball to do finite number of bounces. This is possible because time needed for vertical travel with some number of bottom hits (let's say, Tk) is exactly same as time taken for the other number of horizontal hits with the wall (Tn). In unlucky case if you can't divide one time by the other and get an integer, then the ball would get into infinite loop inside the well.
A: If the ball is a point mass (no angular momentum), then in a top view, the ball will travel along a star pattern, for example like this (this is for $\theta=18$ deg):

Since the ball will not lose energy, it will go on forever, although not necessarily along a closed star pattern like in this picture. The ball makes parabolic trajectories, starting at the top of a parabola. The horizontal component of the velocity of the ball does not change in magnitude, only in direction. The highest points of the parabolas will always be exactly at the height of the edge of the well; the ball is supposed to escape if that highest point happens to be on the edge, i.e., if a vertex of the star coincides with the highest point of the parabola.
So: calculate the length $a$ of a single line segment (they are all the same) and calculate the horizontal distance $b$ traveled by the ball over one full parabola (from highest point to highest point). The ball will come out if there are two integers $m$ and $n$ such that $ma=nb$. You can figure out why they need to be mutually prime.
A: Farcher provides an excellent walkthrough of the problem, but maybe doesn't address your question of: "Why there are some conditions where the ball does not exit even though it loses no energy?" Here's an example of one such condition:
For simplicity say $\theta\ =\ 0$ so the ball will travel directly across the diameter of the well. Imagine the ball falls in and it is going fast ($v$) enough  or the well is deep ($H$) or wide ($r$) enough that by the time the ball first hits the floor of the well it has almost reached the wall opposite the side that it entered. By the time it has rebounded to its original height at the top of the well it will not be quite at the edge of the well, and so will be unable to escape and will fall back down and continue bouncing. You could imagine that the conditions $v$, $H$, and $r$ are such that it repeats a cycle forever, never at the edge of the well when it is at maximum height.
A: You've had one side of the answer: you need to have synchrony in order to exit. But will it ever happen?
There you need some maths: you need to find integers $n,\,k$ such that $n/k = A$, with $A$ some real number depending on parameters. Since $A$ is a real number, in general no such $n$ and $k$ exist... but since the set of rational numbers is dense in the set of real numbers, you can go infinitely close to $A$. In other words, the ball will exit after infinite time, but it will exit.
A: taking gravity in mind, the ball takes following time to reach the well bottom surface. And it will take the same amount of time to return to the top surface.  $$T=\sqrt{2H/g}$$
So its chance to escape is only at $$2T, 4T, 6T...2nT$$
In other time, it is travelling to the bottom face and back to the top face. 
Horizontally, you want the ball at the edge of the circle when it reaches the top surface. If the ball is not on the edge, it will fall back. So we need $$k\times 2r\cos \theta = v*2nT$$. This gives the answer.
A: Looking down from the top you can see the symmetry of the situation with the ball hitting a wall in a time interval of $\dfrac {2 r \cos \theta}{v}$ and for vertical motion the ball again reaching the rim in a time $2 \sqrt{\dfrac {2H}{g}}$ (using $s = \frac 1 2 g t^2$).

So you must have $\dfrac{2 n r\cos\theta}{v}=2 k \sqrt{\frac{2H}{g}}$ for the hitting the wall and reaching the maximum height to occur simultaneously where $n$ and $k$ are integers.

Later:
If the ball is a point mass and arrives right at the top of the wall it will not hit the wall and rebound but will continue moving in a straight line and so escape from the well.
A: the only way for the ball to escape is if it bounces a number of times and it reaches the edge at the top of a bounce. This condition means that the horizontal time flight must be a rational fraction of the vertical time: $t_h=\frac{m}{k}t_v$, which includes all possibilities, including coming back to the initial point, or getting out through the other side, hole large enough to allow several horizontal bounces, or too small to allow one before bouncing on the wall before touching the floor. 
A: Answering the question: Why do we need a condition for the ball to come out of the well?


There will be only specific points in time when the ball is at its highest point (red dots). if the ball has constant horizontal velocity, then each time the ball is at its maximum point, the same horizontal distance will be covered by the ball (A = B = C). Also, the distance covered between each bounce from the wall and the next will be equal, since they will bounce off at the same angle when viewed at the top (a = b).
 therefore, we must satisfy the condition
let $u$ be the horizontal distance covered every two consecutive highest point (real number), 
let $w$ be the horizontal distance between two consecutive bounce from the wall (real number).
let $n$ be a positive integer which denotes how many times the maximum height is reached 
let $m$ be a positive integer which is the number of times the ball bounces the wall.
we must satisfy this condition to make the ball go out:
$nu = mw$
or
$n/m=w/u$
The left side must be a rational number, which can be achieved by the ratio of any 2 integers, while the right side is a real number, which can be irrational and cannot be represented as a ratio of any 2 integer.
 So in order for the ball to bounce out with any angle and speed, you must satisfy that w/u can be represented as a ratio of two integers.
EDIT (After noticing that my answer is the same as others):
You're Right, the ball should come out, if it is not a point object:

 Part of the horizontal component could easily become part of the vertical component after bouncing the edge.
