How to solve second problem STDDEV of acm icpc online round

int n,q;

their product can overflow when they both are of {10}^{5}. Additionally make sure you are printing atleast 3-4 decimal places.

1 Like

@vijju123 :slight_smile: even I forgot that!! Thats why I remebered that I wrote the case for sigma=0

Yes, I am sorry for the confusion @bhola_nit :slight_smile:

Omg!! why didn’t check for overflow? I was more concerned about some corner cases. :frowning:

It makes sense to worry in some cases. But remember, output was around 10^8. You were allowed to make an error of 10^5+1e-2. You can analyze and see that intermediate values won’t explode so much.

That’s easy. Basically, my idea is as follows. I want mean 0 and almost all the values a_i = 1, so the variance would be 1. If n is even, then keeping half 1’s and the half -1’s does the job.

Consider the case when n is odd. This is bit tricky case. Here you have to keep (n-1)/2 1’s and -1’s, let the remaining three numbers be x, y, z. We want x+y+z = 0 and x^2 + y^2 + z^2 = 3. Simplifying, we get z = -(x+y).

Now, we make further assumption that what if x = y.

We have z = -2*x, Solving eqs, x^2 = 0.5 \implies x = \sqrt(0.5), y = sqrt(0.5) and z=-2\times \sqrt(0.5)

Yes, the N being odd was troubling me. Now I can be in peace <3 :slight_smile:

@admin: Yes, I tested. They arent exploding much. Well, I just thought about playing it safe. Thanks for explaining it, I really appreciate this :slight_smile: <3

What I did was if n was even I printed -sigma n/2 times and sigma n/2 times. This makes the mean 0 and std=sqrt(n*sigma^2/sigma) = sigma.

If n was odd (n=1 handled separately) print last element as 0 and (n-1)/2 elements as sigma * sqrt(n/n-1) and the remaining as (-1 * sigma* sqrt(n/n-1)). Again makes mean 0 and then the usual stuff.

2 Likes

Same here :slight_smile:
The first thing that struck me was if all elements are at a fixed distance from the mean, the standard deviation will naturally equal that distance. This gave a solution for even N, and the adjustment for odd N followed.

1 Like

Thanks @admin . I will keep it in my mind from the next time.

What I did was I randomly filled array of size N with random numbers within 10^5. Calculated standard deviation of that array in linear time(while populating it), then just multiplied all elements by x, (x = required SD / current SD of array ). Why do we need an array with SD 1? SD X also works.

5 Likes

nice approach ! @swetankmodi

@swetankmodi: You just proved us too dumb. Thanks for such a nice approach :slight_smile: Totally missed this up. In the version, I had thought of allowing only integers in the array. That’s why this line of thought was more stuck.

3 Likes

@admin : glad it helped :slight_smile: and allowing only integers would have been more challenging :slight_smile: @jaideeppyne thanks

@swetankmodi ,only 1 word :PRESSURE=RIP THINKING XDDD. Damn, since doubles are allowed, its so true :3

Damn, that appeoach is sexy XD.

2 Likes

Another possible Approach which was used by our team is as follows. This is different from all other approaches because in this case each number generated is distinct i.e. no two numbers are same.

Let us try to generate an Arithmetic Progression :

k, 2k, 3k, 4k,…,nk whose standard deviation is \sigma. where k is some real number.

The value of k can be derived in about 6-7 lines by solving the equation of Standard Deviation which is left as an exercise for the reader. ( :stuck_out_tongue: )

For n=1 , denominator( for eqn of k) turns out to be zero which indicated “-1” for the case when n=1.