Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
The original question is:
Describe an algorithm to output a die roll(a random number from 1 to 6), given a function that outputs a coin toss(a random number from 1 to 2). Each possible outcome should be equally likely.
The most popular answer to this question is:
Flip the coin three times, and use the three coin flips as the bits of a three-bit number. If the number is in the range 1 to 6, output the number. Otherwise, repeat.
My question is:
Most of the discussions on Stack Overflow come in the above flavour. I've also searched the Internet, finding that there exist many answers of other flavours, which they don't explicitly dig into. Could someone please share one or two different ideas on this problem?
There is a small improvement to the "toss 3 times and discard if 110 or 111" algorithm. Discarding 110 or 111 is wasteful, since you are wasting one perfectly good bit of entropy that you could reuse. After one of these values pops up, you only need to toss twice and get the value of the third toss from the mapping {110->tails, 111->heads}. Both of 110 and 111 are equally probable, so you're not introducing any bias this way.
In pseudocode:
bit0 = toss()
while True:
bit1 = toss()
bit2 = toss()
if bit1,bit2,bit3 give i such that 0<=i<=5 then
return i+1
else
bit0 = bit3 // the reuse happens here
The expected number of tosses here is 1 + 2 * expected_number_of_loop_executions = 1+2*8/6 = 11/3
If you just want other options, not necessarily good ones then how about this:
Flip a coin for each possible output value.
If there is one or more heads then discard all the possible values that got tails.
If you have only one value left then stop. Else goto 1.
I would suspect it is going to have a higher expected number of coin tosses than the method you've described and have no advantages at all really.
In general I assume this is why there is not much on the other possible ways of using random numbers. They are just not as good.
Flip 5 coins. If they're all heads or all tails, your answer is 1. If there's only one head or one tail, continue to the next step. If there's more than one head and more than one tail, repeat this step (including reflipping the coins).
Flip 4 coins. If they're all heads or all tails, your answer is 2. If there's only one head or one tail, continue to the next step. If there's two heads and two tails, repeat this step (including reflipping the coins).
Flip 3 coins. If they're all heads or all tails, your answer is 3. Otherwise, continue to the next step
Flip 2 coins. If they're both heads, your answer is 4. If they're both tails, repeat this step (including reflipping the coins). Otherwise, continue to the next step.
Flip 1 coin. If it's heads, your answer is 5. If it's tails, your answer is 6.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Call every subunitary ratio with its denominator a power of 2 a perplex.
Number 1 can be written in many ways as a sum of perplexes.
Call every sum of perplexes a zeta.
Two zetas are distinct if and only if one of the zeta has as least one perplex that the other does not have. In the image shown above, the last two zetas are considered to be the same.
Find all the numbers of ways 1 can be written as a zeta with N perplexes. Because this number can be big, calculate it modulo 100003.
Please don't post the code, but rather the algorithm. Be as precise as you can.
This problem was given at a contest and the official solution, written in the Romanian language, has been uploaded at https://www.dropbox.com/s/ulvp9of5b3bfgm0/1112_descr_P2_fractii2.docx?dl=0 , as a docx file. (you can use google translate)
I do not understand what the author of the solution meant to say there.
Well, this reminds me of BFS algorithms(Breadth first search), where you radiate out from a single point to find multiple solutions w/ different permutations.
Here you can use recursion, and set the base case as when N perplexes have been reached in that 1 call stack of the recursive function.
So you can say:
function(int N <-- perplexes, ArrayList<Double> currentNumbers, double dividedNum)
if N == 0, then you're done - enter the currentNumbers array into a hashtable
clone the currentNumbers ArrayList as cloneNumbers
remove dividedNum from cloneNumbers and add 2 dividedNum/2
iterate through index of cloneNumbers
for every number x in cloneNumbers, call function(N--, cloneNumbers, x)
This is a rough, very inefficient but short way to do it. There's obviously a lot of ways you can prune the algorithm(reduce the amount of duplicates going into the hashtable, prevent cloning as much as possible, etc), but because this shows the absolute permutation of every number, and then enters that sequence into a hashtable, the hashtable will use its equals() comparison to see that the sequence already exists(such as your last 2 zetas), and reject the duplicate. That way, you'll be left with the answer you want.
The efficiency of the current algorithm: O(|E|^(N)), where |E| is the absolute number of numbers you can have inside of the array at the end of all insertions, and N is the number of insertions(or as you said, # of perplexes). Obviously this isn't the most optimal speed, but it does definitely work.
Hope this helps!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have been stuck on this uva problem from a long time now.
Abridged problem statement : Given a set of sticks of various lengths, is it possible to join them end-to-end to form a square? There are a maximum of 20 sticks and each stick has a length less than 10000.
There are different solutions possible for this problem. One of them is a backtracking solution explained here. But there exists other dynamic programming solutions explained here, here and here with a better running time. But I can't understand what approach they are using. Please help me understand the dp algorithm.
If you are not familiar with dynamic programming over subsets, I suggest you read about it first. This link can help, but there may be better tutorials out there.
Back to the given problem, since M is no more than 20, the following 2M×M approach will probably work.
For each of the 2M subsets of the given sticks, we know the total length of the sticks in that subset. We also know the total length of all the given sticks and thus the length of the square side. We construct the square by laying sticks on its sides. Let us fix the order in which we construct our square: we start at the upper left corner and move along the square border in clockwise direction, laying sticks on the way and leaving no gaps. So, first we fully construct the upper side (from left to right), then the right side (top-down), then the lower one (right-to-left) and finally the left one (bottom-up). Whenever the distance to the next square corner in our traversal is L, we can't lay a stick of length greater than L; at least not until we reach that corner using other sticks. Now, the question is: can we order the sticks in such a way that the square can be constructed by our procedure?
There are M! different orders in which we can try to lay the sticks. But, if we lay sticks one-by-one, when choosing the next stick, all that we are concerned with is the set of sticks already laid, not their particular order. This observation leads us to considering only 2M subsets which is way smaller than M! orders.
Next we define subproblems of the problem defined before. For each subset of sticks, the question is: can we order the sticks in such a way that all of them can be laid sequentially by the rules of the above procedure? In other words, can we construct a "valid prefix" of the square traversal, as it is defined above?
We will say a subset of sticks is good if the answer to the above question is "yes", and bad otherwise. Sure, the empty subset is good. In the end, we are interested in whether the whole given set of sticks is also good. To find that out, we can process the subsets in natural order (for M=3, that would be 000, 001, 010, 011, 100, 101, 110, 111 where 1s correspond to sticks in the subset). For each new non-empty subset S, it is good if and only if some of its "immediate subsets" T (S without exactly one element - say a stick of length X) is good, and T can be extended by that stick of length X according to the rules of our construction procedure (that is, laying a stick of length X, we won't have to bend it around some corner).
What's left is implementation details. For each subset, either store or calculate the total length of the sticks in it and find the distance to the next corner L. If this subset is good, it can be extended only by sticks having two properties: (1) length no more than L and (2) not already in the subset.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am trying to figure out a way to figure out how many times an even number can be divided by two before it becomes odd. I.E. 100/2 = 50 => 50/2 = 25. So 100 can be divided by two twice. before it becomes odd. I am looking for all even numbers that are NOT powers of two.
That's two questions.
I am trying to figure out a way to figure out how many times an even number can be divided by two before it becomes odd. I.E. 100/2 = 50 => 50/2 = 25. So 100 can be divided by two twice. before it becomes odd.
Convert the number binary and count the 0s before the first 1. Each time you dive by 2 you loose a zero and when a 1 is in the "first" position you've got an odd number.
I am looking for all even numbers that are NOT powers of two.
Again, looking at the binary, powers of 2 have only one bit set and odd numbers have the 1 bit set. So anything with multiple bits set AND not the 1 bit is your answer.
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
Given: A set of n piles of coins (n <= 100), such that their XOR-sum (nim-sum) is not 0. Each pile can contains coins which can be represented by 30 bits.
Desired: To get an overall XOR (nim sum) of 0.
Restriction: Stones can be removed from any of the piles, as long as atleast one pile is left unchanged. Stones cannot be added to any pile.
Required Output: The number of ways in which this can be achieved, modulo a large prime.
I understand that leaving one pile unchanged means that the XOR of the remaining piles must be equal to the coins in the unchanged pile. I was trying to use Dynamic Programming, but got nowhere with it. One tricky aspect seems to be that any partial solution with some pile unchanged may be counted more than once when considering some other pile that had also remained unchanged. This was a contest question, and the contest is over now. Any help would be greatly appreciated. Thanks!
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
This is an interview question, not a homework.
N friends are playing a game. Each of them has a list of numbers in front of himself.
Each of N friends chooses a number from his list and reports it to the game administrator. Then the game administrator sorts the reported numbers and shouts the K-th largest number.
I must the count all possible numbers that the game administrator can shout.
For example, if N = 3 and K = 3, and the lists for the 3 friends are 2 5 3, 8 1 6, 7 4 9. The output is 6, since the possible values are 4 5 6 7 8 9.
Can anybody suggest a decent algorithm for this problem? What I am doing is to create all possible permutations taking one number from each list, then sorting the resultant and printing the kth-largest. But that takes too much time.
To know if a number is present in the result or not, you need to know for each other list if there are numbers above and if there are numbers below. List where there are both numbers above and below are not a problem as you can choose a number in them as it suits you. The problem are lists where there are only numbers above or only numbers below. The first ones must be at most N-K, the second ones must be at most K. If this is not true, your number cannot be picked. If this is true, you can always choose numbers in the lists where there are both number above and below so that your number is picked.
This can be checked in linear time, or even better if your first sort your lists, thus giving an overall complexity of O(n.log(n)) where n is the number of numbers in total. Without sorting you got a n² complexity.
In your example with lists :
{2 5 3}, {8 1 6}, {7 4 9}
say we are looking for the 2-greatest number. For each number we ask if it can be shout by the administator. For each of them we look if in the other list there are both numbers below and numbers above. Let's look further for some of them
For 5 : there is numbers above and below in both other lists. So "5" can be shout by the administrator.
For "2" : there is numbers above and below in the second list so I can freely choose a number above or below in this list. But there are only numbers above in the third list, so the picked number in this list will always be greater. Since i can freely choose a number below in the second list, thus making my "2" the 2nd greatest number.
For "1" : there is only numbers above in the second list, so "1" will always be the smallest element.
For "9" : this is the other way round, it is always the greatest.
take the smallest number from each set. find the K-th -largest of these. This is the smallest number that is in the result. Similarly, find the largest number in the result. Prove that Each number between the two is in the result.