Count ways to reduce nim piles to get XOR sum 0 [closed] - algorithm

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!

Related

Which number appeared once? [closed]

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 6 years ago.
Improve this question
Given a list of 2n-1 numbers: all between 1 to n, all but one occur twice. Determine the number that occurs only once. Multiple ways preferred.
I think the problem is at fault, how can you determine which number without knowing the list of numbers?
[O(1) space, O(n) time]: Just take the XOR of all the numbers. Since all the numbers occur two times except one, XOR of those numbers will be zero and the single occurring number will be the result.
[O(1) space, O(n) time]: As said by user3386109 in comments, we can sum all the given numbers and compare that to the sum of numbers in the range [1, n] which will be n*(n+1) (since all numbers are supposed to occur twice). The difference of the two numbers is the answer.
[O(n) space, O(n) time]: Create an array of size n and keep the count of all the elements in the array at their corresponding positions. At the end, traverse the array, and find the number whose count is only 1.

Possible Array combinations based on constraints [closed]

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 6 years ago.
Improve this question
How many unique arrays of m elements exist such that they contain numbers in the range [1,n] and there exists atleast one subsequence {1,2,3,4....n}?
Constraints: m > n
I thought of combinations approach. But there will be repetitions.
In my approach, I first lay out all the numbers from 1 to n.
For example, if m=n+1, answer is n^2. (n spots available, each number in range [1,n])
Now, I think there might be a DP relation for further calculation, but I am not being able to figure it out.
Here's an example for n=3 and m=5. The green squares are the subsequence. The subsequence consists of the first 1 in the array, the first 2 that's after the first 1, etc. Squares that aren't part of the subsequence can either take n values if they are after the end of the subsequence, or n-1 values otherwise.
So the answer to this example is 1*9 + 3*6 + 6*4 = 51, which is easily verified by brute force. The coefficients 1,3,6 appear to be related to Pascal's triangle. The rest is left to the reader.

Is O(nk(log(k))) algorithm same as O(n(log(k))) [closed]

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 8 years ago.
Improve this question
I was asked to give an algorithm that was supposed to be O(n(log(k)))
k is the number of arrays and n is the total number of elements in all of these. I had to sort the arrays.
Minus the details I came up with an algorithm that does the job for in klog(k) times the total number of elements. i.e. O(nk(log(k)))
Also in this case k is much smaller than n so it wont be n^2(logn) (in case k and n were almost same)right?
Well, no, it's not the same. If k is a variable (as opposed to a constant) in the complexity expression then O(nk(log(k))) > O(n(log(k))).
That is because there is no constant C such that Cn(log(k)) > kn(log(k)) for every n, k.
The way you describe the question both k and n are input parameters. If that is the case then the answer to your question is
'No, O(n*k *log(k)) is not the same as O(n*log(k))'.
It is not that hard to see that the first one grows faster than the second one, but it is even more obvious if you fix the value of n. Consider n begin a constant say 1. Than it is more obvious that O(k*log(k)) is not the same as O(log(k)).

How many times can an even number be divided by two before it becomes odd? [closed]

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.

Looking for New Ideas on An Old Interview Trick [closed]

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.

Resources