I am trying to solve the SPOJ Problem EVERLAST, as given here,
The fate of the Pineapple
Currently what I am trying is:
1) After the first season the population = N * K
2) After the second season the population = N * K^2*(1-p)
since p is the probability that each pineapple is sterile and the initial N* K population dies out
3) After the third season the population = N * K^3 * (1-p)^2
a)The probability that pineapple population survives after second season = (1- p^(N * K)), since it dies out completely with a probability p^(N * K)
b)Probability that pineapple population survives after third season = 1 - p^ (N* K^2 *(1-p)), since it dies out completely with a probability p^(N * K^2 * (1-p))
and so on..
so the probability of population surviving forever will be
(1-p^(N * K)) * (1-p^(N * K^2 * (1-p))) * .......
But I am getting the wrong answer, I cant find the hints to this anywhere.
Where am I going wrong in my approach? any hints please?
Related
I am having trouble analyzing the following.
for b = 1 to n
for u = b to n
for i = b to u
Print
I am having trouble determining the worst-case runtime.
The first loop runs n times, the second loop runs (n(n+1))/2 times.
I then beleive that the third loop runs (n+1)/2 times.
However I have been told that it has a runtime of O(n^3).
Therefore it's best-case runtime can't be greater then the worst-case runtime!
I'm hoping for a push in the right direction if possible!
Thanks!
The third loop executes Print u-b+1 times {3} when it is activated.
When the second loop is activated, the third loop is activated for all u from b to n, i.e. executes Print 1+2+3+...n-b+1 times (substitute u=1, 2, 3...n in {3}). Using the formula for triangular numbers, this count equals (n-b+1)(n-b+2)/2 {2}.
When the first loop is activated, the second loop is activated for all b from 1 to n, i.e. executes Print n(n+1)/2 + (n-1)n/2 + (n-2)(n-1)/2 + ... 1 times (substitute b= 1, 2, 3... n in {2}; the counts are decreasing).
Using the formula for tetrahedral numbers, this equals n(n+1)(n+2)/6 {1}.
If you have trouble with this approach, you can simulate the Prints by hand instead.
For n=1
*
For n=2
*
* *
*
For n=3
*
* *
* * *
*
* *
*
For n=4
*
* *
* * *
* * * *
*
* *
* * *
*
* *
*
...
You clearly see the pattern of triangles of decreasing size. The area of the triangles grows as the square of the side. The total volume grows as the cube of the side.
I have come across this question:
Let 0<α<.5 be some constant (independent of the input array length n). Recall the Partition subroutine employed by the QuickSort algorithm, as explained in lecture. What is the probability that, with a randomly chosen pivot element, the Partition subroutine produces a split in which the size of the smaller of the two subarrays is ≥α times the size of the original array?
Its answer is 1-2*α.
Can anyone explain me how has this answer come?Please Help.
The choice of the pivot element is random, with uniform distribution.
There are N elements in the array, and we will assume that N is large (or we won't get the answer we want).
If 0≤α≤1, the probability that the number of elements smaller than the pivot is less than αN is α. The probability that the number of elements greater than the pivot is less than αN is the same. If α≤ 1/2, then these two possibilities are exclusive.
To say that the smaller subarray is of length ≥αN, is to say that neither of these conditions holds, therefore the probability is 1-2α.
The other answers didn't quite click with me so here's another take:
If at least one of the 2 subarrays must be you can deduce that the pivot must also be in position . This is obvious by contradiction. If the pivot is then there is a subarray smaller than . By the same reasoning the pivot must also be . Any larger value for the pivot will yield a smaller subarray than on the "right hand side".
This means that , as shown by the diagram below:
What we want to calculate then is the probability of that event (call it A) i.e .
The way we calculate the probability of an event is to sum of the probability of the constituent outcomes i.e. that the pivot lands at .
That sum is expressed as:
Which easily simplifies to:
With some cancellation we get:
Just one more approach for solving the problem (for those who have uneasy time understanding it, like I have).
First.
Since we are talking about "the smaller of the two subarrays", then its length is less than 1/2 * n (n - the number of elements in original array).
Second.
If 0 < a < 0.5 it means the a * n is less than 1/2 * n either.
And thus we are talking from now about two randomly chosen integers bounded by 0 at lowest and 1/2 * n at highest.
Third.
Lets imagine the dice with numbers from 1 to 6 on it's sides. Lets choose a number from 1 to 6, for example 4. Now roll the dice. Each number has a probability 1/6 to be the outcome of this roll. Thus for event "outcome is less or equal to 4" we have probability equal to the sum of probabilities of each of this outcomes. And we have numbers 1, 2, 3 and 4. Altogether p(x <= 4) = 4 * 1/6 = 4/6 = 2/3. So the probability of event "output is bigger than 4" is p(x > 4) = 1 - p(x <= 4) = 1 - 2/3 = 1/3.
Fourth.
Lets go back to our problem. The "chosen number" is now a * n. And we are going to roll the dice with the numbers from 0 to (1/2 * n) on it to get k - the number of elements in a smallest of subarrays. The probability that outcome is bounded by (a * n) at highest is equals to sum of the probabilities of all outcomes from 0 to (a * n). And the probability for any particular outcome k is p(k) = 1 / (1/2 * n).
Therefore p(k <= a * n) = (a * n) * (1 / (1/2 * n)) = 2 * a.
From this we can easily conclude that p(k > a * n) = 1 - p(k <= a * n) = 1 - 2 * a.
Array length is n.
For smaller array length >= αn pivot should be greater than αn number of elements. At the same time pivot should be smaller than αn number of elements( else smaller array size will be less than required)
So out of n element we have to select one among (n-2α)n elements.
required probability is n(1-2α)/n.
Hence 1-2α
The probability would be, the number of desired elements/Total number of elements.
In this case, ((1-αn)-(αn))/n
Since α lies between,0 and 0.5,(1-α) must be bigger than α.Hence the number of elements contained between them would be,
(1-α-α)n=(1-2α)n
and so,the probability would be,
(1-2α)n/n=1-2α
Another approach:
List the "more balanced" options:
αn + 1 to (1 - α)n - 1
αn + 2 to (1 - α)n - 2
...
αn + k to (1 - α)n - k
So k in total. We know that the most balanced is n / 2 to n / 2, so:
αn + k = n / 2 => k = n(1/2 - α)
Similarly, list the "less balanced" options:
αn - 1 to (1 - α)n + 1
αn - 2 to (1 - α)n + 2
...
αn - m to (1 - α)n + m
So m in total. We know that the least balanced is 0 to n so:
αn - m = 0 => m = αn
Since all these options happen with equal probability we can use the frequency definition of probability so:
Pr{More balanced} = (total # of more balanced) / (total # of options) =>
Pr{More balanced} = k / (k + m) = n(1/2 - α) / (n(1/2 - α) + αn) = 1 - 2α
Given k, we need to write 1 as a sum of k fractions of the form 1/r.
For example,
For k=2, 1 can uniquely be written as 1/2 + 1/2.
For k=3, 1 can be written as 1/3 + 1/3 + 1/3 or 1/2 + 1/4 + 1/4 or 1/6 + 1/3 + 1/2
Now, we need to consider all such set of k fractions that sum upto 1 and return the highest denominator among all such sets; for instance, the sample case 2, our algorithm should return 6.
I came across this problem in a coding competition and couldn't come up with an algorithm for the same. A bit of Google search later revealed that such fractions are called Egyption Fractions but probably they are set of distinct fractions summing upto a particular value (not like 1/2 + 1/2). Also, I couldn't find an algo to compute Egyption Fractions (if they are at all helpful for this problem) when their number is restricted by k.
If all you want to do is find the largest denominator, there's no reason to find all the possibilities. You can do this very simply:
public long largestDenominator(int k){
long denominator = 1;
for(int i=1;i<k;i++){
denominator *= denominator + 1;
}
return denominator;
}
For you recursive types:
public long largestDenominator(int k){
if(k == 1)
return 1;
long last = largestDenominator(k-1);
return last * (last + 1); // or (last * last) + last)
}
Why is it that simple?
To create the set, you need to insert the largest fraction that will keep it under 1 at each step(except the last). By "largest fraction", I mean by value, meaning the smallest denominator.
For the simple case k=3, that means you start with 1/2. You can't fit another half, so you go with 1/3. Then 1/6 is left over, giving you three terms.
For the next case k=4, you take that 1/6 off the end, since it won't fit under one, and we need room for another term. Replace it with 1/7, since that's the biggest value that fits. The remainder is 1/42.
Repeat as needed.
For example:
2 : [2,2]
3 : [2,3,6]
4 : [2,3,7,42]
5 : [2,3,7,43,1806]
6 : [2,3,7,43,1807,3263442]
As you can see, it rapidly becomes very large. Rapidly enough that you'll overflow a long if k>7. If you need to do so, you'll need to find an appropriate container (ie. BigInteger in Java/C#).
It maps perfectly to this sequence:
a(n) = a(n-1)^2 + a(n-1), a(0)=1.
You can also see the relationship to Sylvester's sequence:
a(n+1) = a(n)^2 - a(n) + 1, a(0) = 2
Wikipedia has a very nice article explaining the relationship between the two, as pointed out by Peter in the comments.
I never heard of Egyptian fractions before but here are some thoughts:
Idea
You can think of them geometrically:
Start with a unit square (1x1)
Draw either vertical or horizontal lines dividing the square into equal parts.
Repeat optionally the drawing of lines inside any of the sub-boxes evenly.
Stop any time you want.
The rectangles present will form a set of fractions of the form 1/n that add to 1.
You can count them and they might equal your 'k'.
Depending on how many equal sections you divided a rectangle into, it will tell whether you have 1/2 or 1/3 or whatever. 1/6 is 1/2 of 1/3 or 1/3 of 1/2. (i.e. You dived by 2 and then one of the sub-boxes by 3 OR the other way around.)
Idea 2
You start with 1 box. This is the fraction 1/1 with k=1.
When you sub-divide by n you add n to the count of boxes (k or of fractions summed) and subtract 1.
When you sub-divide any of those boxes, again, subtract 1 and add n, the number of divisions. Note that n-1 is the number of lines you drew to divide them.
More
You are going to start searching for the answer with k. Obviously k * 1/k = 1 so you have one solution right there.
How about k-1?
There's a solution there: (k-2) * 1/(k-1) + 2 * (1/((k-1)*2))
How did I get that? I made k-1 equal sections (with k-2 vertical lines) and then divided the last one in half horizontally.
Each solution is going to consist of:
taking a prior solution
using j less lines and some stage and dividing one of the boxes or sub-boxes into j+1 equal sections.
I don't know if all solutions can be formed by repeating this rule starting from k * 1/k
I do know you can get effective duplicates this way. For example: k * 1/k with j = 1 => (k-2) * 1/(k-1) + 2 * (1/((k-1)*2)) [from above] but k * 1/k with j = (k-2) => 2 * (1/((k-1)*2)) + (k-2) * 1/(k-1) [which just reverses the order of the parts]
Interesting
k = 7 can be represented by 1/2 + 1/4 + 1/8 + ... + 1/(2^6) + 1/(2^6) and the general case is 1/2 + ... + 1/(2^(k-1)) + 1/(2^(k-1)).
Similarly for any odd k it can be represented by 1/3 + ... + 3 * [1/(3^((k-1)/2)].
I suspect there are similar patterns for all integers up to k.
I'm working on proving something regarding probability and statistics.
How many time I need to pick a random number from [1,N] to get specific number k, where k in [1,N]
start = random(1,N);
count = 1;
do
{
end = random(1,N);
count++;
}while (start!=end);
My experiments concluded that if I repeat above program for 100 time for the same N value then average value of count ~ N. I don't know how to prove that theoretically.
Any one can help me to prove it. Any help would be appreciated.
Each time you pick a random number between 1 and N, you have a probability of getting k which is equal to 1/N, and a probability of getting something different which is equal to (N-1)/N.
Once you know this, you can compute the probability of getting k in :
1 shot : P1 = 1/N
2 shots : P2 = (N-1)/N * 1/N
3 shots : P3 = (N-1/N * (N-1)/N * 1/N
...
The expected number of times you have to pick a random number in order to get k is:
1 * P1 + 2 * P2 + 3 * P3 + 4 * P4...
This is a series that converges to the value N.
I am trying to understand how many times the statement "x = x + 1" is executed in the code below, as a function of "n":
for (i=1; i<=n; i++)
for (j=1; j<=i; j++)
for (k=1; k<=j; k++)
x = x + 1 ;
If I am not wrong the first loop is executed n times, and the second one n(n+1)/2 times, but on the third loop I get lost. That is, I can count to see how many times it will be executed, but I can't seem to find the formula or explain it in mathematical terms.
Can you?
By the way this is not homework or anything. I just found on a book and thought it was an interesting concept to explore.
Consider the loop for (i=1; i <= n; i++). It's trivial to see that this loops n times. We can draw this as:
* * * * *
Now, when you have two nested loops like that, your inner loop will loop n(n+1)/2 times. Notice how this forms a triangle, and in fact, numbers of this form are known as triangular numbers.
* * * * *
* * * *
* * *
* *
*
So if we extend this by another dimension, it would form a tetrahedron. Since I can't do 3D here, imagine each of these layered on top of each other.
* * * * * * * * * * * * * * *
* * * * * * * * * *
* * * * * *
* * *
*
These are known as the tetrahedral numbers, which are produced by this formula:
n(n+1)(n+2)
-----------
6
You should be able to confirm that this is indeed the case with a small test program.
If we notice that 6 = 3!, it's not too hard to see how this pattern generalizes to higher dimensions:
n(n+1)(n+2)...(n+r-1)
---------------------
r!
Here, r is the number of nested loops.
The 3rd inner loop is the same as the 2nd inner loop, but your n is a formula instead.
So, if your outer loop is n times...
and your 2nd loop is n(n+1)/2 times...
your 3rd loop is....
(n(n+1)/2)((n(n+1)/2)+1)/2
It's rather brute force and could definitely be simplified, but it's just algorithmic recursion.
The mathematical formula is here.
It is O(n^3) complexity.
This number is equal to the number of triples {a,b,c} where a<=b<=c<=n.
Therefore it can be expressed as a Combination with repetitions.. In this case the total number of combinations with repetitions is: n(n+1)(n+2)/6
1 + (1+2) + (1+ 2+ 3 ) +......+ (1+2+3+...n)
You know how many times the second loop is executed so can replace the first two loops by a single one right? like
for(ij = 1; ij < (n*(n+1))/2; ij++)
for (k = 1; k <= ij; k++)
x = x + 1;
Applying the same formula you used for the first one where 'n' is this time n(n+1)/2 you'll have ((n(n+1)/2)*(n(n+1)/2+1))/2 - times the x = x+1 is executed.