Expectation of the Sum of K Numbers [closed] - algorithm

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 working on a histogram problem, and then I ran into the following math problem:
Given N numbers, where the value of each number is different, denoted as v1, v2, ..., vn, and the probability of selecting each number is p1, p2, ..., pn, respectively.
Now if I select K numbers based on the given probabilities, where K <= N, what is the expectation of the sum of those K numbers? Note that the selection is without replacement, so that the K numbers cannot involve duplicate numbers. I understand that if the selection is with replacement, the expectation of the sum of the K numbers equals K * E(V), where E(V) = v1*p1 + v2*p2 + ... + vn*p2.
Furthermore, what about the expectation of the variance of those K numbers?
This question is better formulated at here.

Related

How can I find Kth element in a m*n matrix that each rows(m) are in order with O(m(log m +log n)) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
suppose that we have a m*n matrix that each rows are in order. so, i only know that order of best algorithm for this problem is O(m(log m + log n)).
(It was a test question and result is this order)
but i don't know how this algorithm works
One idea can be like this.
If I ask you what is the rank of a given number x in the original matrix? How do you answer this question?
One answer can be:
Just binary search the first occurrence of x or greater element on each row. and then add the individual ranks.
int rank = 1;
for (int i = 0; i < m; ++i) {
rank += std::lower_bound(matrix[i].begin(), matrix[i].end(), x);
}
This can be done in O(m * log n) time(m binary searches on n sized arrays).
Now we just need to do a binary search on x(between 0 and INT_MAX or matrix[0][k]) to find the kth rank. Since INT_MAX is const, that will make the overall time complexity O(m * log n) theoretically. One optimization, which can be done use intelligent ranges in place of matrix[i].begin(), matrix[i].end().
PS: Still wondering the O(m*(log m + log n)) or O( m * (log mn)) solution.

Cutting a rod dynamic programming [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
Given a rod of length n inches and an array of prices that contains prices of all pieces of the size smaller than n. Using dynamic programming we can get the maximum value and corresponding pieces of the rod. Is there any algorithm which will produce kth maximum value with the corresponding cut for this problem?
Problem : Find k th max price for rod cutting problem.
I think the algorithm can be tweaked in following way :
Change the recursion in rod cutting problem from :
cutRod(n) = max(price[i] + cutRod(n-i-1)) for all i in {0, 1 .. n-1}
To :
Top_K_Price_CutRod(n)[] = top_k(price[i] + cutRod(n-i-1)) for all i in {0, 1 .. n-1}
Basically, at every recursion step, return max k prices for that subpart, because only those can be eventually in overall maximum k .
Bruteforce way is to return all the possible prices of that subpart, but we know for sure that prices which have rank greater than k for this subpart, can't be in top k prices of complete rod eventually.
So here Top_K_Price_CutRod(n)[] is an array of k max prices for that subpart.
At root of the recursion, you will be left with max top k prices.
I don't think we can optimize more than this, as at every recursion node its top k value can be among final top k. So u have to top k values for all subparts.

Modulo equation, retrieve K from d [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
Here's an equation:
d is the multiple inverse of 3 modulo K.
Assuming I have d, can I find K?
Also, K is not necessarily prime.
Thanks!
You know that
d*3 = 1 (mod K)
this means
d*3 = 1 + n*K
independently of K this however means
d*3 = 1 (mod n)
i.e. that d is the inverse of 3 modulo n too, thus the answer is in general not unique (actually you can use any divisor of nK as answer).

Total number of Subsets [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 9 years ago.
Improve this question
I am getting curious about this question..
How many ways can you choose a subset from the set P = {1, 2, 3, .. n}? The subset S should meet the following condition:
When you choose x (x ∈ P, x is an element of the set P) to create S, you cannot choose a * x and b * x for S.
Constraints :
1 <= n <= 1000
2 <= a < b <= n
b % a != 0 ( b is not divisible by a)
Example :
n = 3 , a = 2, b = 3
so total subsets are 5 ,i.e, {}, {1}, {2}, {3}, {2, 3}
as if in a particular subset there is 1 so 1*2 = 2 and 1*3 cant be there.
so {1,2}, {1,3} and {1,2,3} can't be there
Updated
This is related to sequence A051026 : Number of primitive subsequences of {1, 2, ..., n} in OEIS, the Online Encyclopedia of Integer Sequences.
I don't think there is any easy way to calculate the terms. Even the recursive computations are not trivial, except when n is prime where:
a(n) = 2 * a(n-1) - 1
Both the problem here and "A051026" can be thought of subproblems of a generalization of the above sequence. "A051026" is the instance with (a,b,..) = (2,3,4,5...), e.g. "all the integers >= 2".
I believe it will be easier to calculate the complimentary- that is the number of subsets of S that are not allowed. This is the number of subsets of S for each there is at least one pair (a,b) such that a divides b. After you calculate that number M' simply subtract it from the total number of subsets of S that is 2n.
Now to calculate the number of subsets of S that are not allowed you will have to apply the inclusion-exclusion principle. The solution is not very easy to implement but I don't think there is an alternative approach.

How to represent the max power possible [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
Kind of a math question, but very programming related. Doing some Big-O problems and I have an algorithm where a for loop will run n times, where k = input size, n = max power of 4 where (k)/(4^n) >= 1. How can I represent max power of 4 where (k)/(4^n) >= 1 in one mathematic statement?
floor ( (log k)/(log 4) ).
Or something along those lines.
Mathematic statement: [log_4(k)]
Code: floor( log(k) / log(4) )
log base 4 of k? Can take the floor if you only care about integer n.
Taking (k)/(4^n) >= 1, multiply both sides by 4^n to get k >= 4^n, and then take the log base 4 (log_4) of both sides to get log_4 k >= n, or n <= log_4 k. (Equivalently, take log of both sides and get log k >= log(4^n), then note log(4^n) = n log(4), and divide to get (log k)/(log 4) >= n). Choose the largest integer n satisfying this inequality, which is floor(log_4 k).

Resources