Let’s take 13 as the value of NN and try to calculate the expected value (EV13EV_{13}).

EV13=1%1  +  2%1  +  ...  +  13%113×13+1%2  +  2%2  +  ...  +  13%213×13......+1%13  +  2%13  +  ...  +  13%1313×13EV_{13} = \dfrac{1\%1 \; + \; 2\%1 \; + \; ... \; + \; 13\%1}{13×13} \\ + \dfrac{1\%2 \; + \; 2\%2 \; + \; ... \; + \; 13\%2}{13×13}\\ ... \\ ... \\+ \dfrac{1\%13 \; + \; 2\%13 \; + \; ... \; + \; 13\%13}{13×13}

numerator={1%1  +  2%1  +  ...  +  13%1}+...+{1%13  +  2%13  +  ...  +  13%13}numerator = \{1\%1 \; + \; 2\%1 \; + \; ... \; + \; 13\%1 \} + ... + \{1\%13 \; + \; 2\%13 \; + \; ... \; + \; 13\%13 \}

={0+0+...+0}+{1+0+1+0+...+1}+{1+2+0+1+2+0+1+...+1}+...+{1+2+...+12+0}= \{0 + 0 + ... + 0\} + \{1 + 0 + 1 + 0 + ... + 1\} + \{1 + 2 + 0 + 1 + 2 + 0 + 1 + ... + 1\} + ... + \{1 + 2 + ... + 12 + 0\}

={131×(0)}+{132×(1)+1}+{133×(1+2)+1}+{134×(1+2+3)+1}+{135×(1+2+3+4)+1+2+3}...= \{\lfloor\frac{13}{1}\rfloor × (0)\}\\ + \{\lfloor\frac{13}{2}\rfloor × (1) + 1\}\\ + \{\lfloor\frac{13}{3}\rfloor × (1 + 2) + 1\}\\ + \{\lfloor\frac{13}{4}\rfloor × (1 + 2 + 3) + 1\}\\ + \{\lfloor\frac{13}{5}\rfloor × (1 + 2 + 3 + 4) + 1 + 2 + 3\}\\ ...

={131×i=00i}+{132×i=01i+i=013%2i}+{133×i=02i+i=013%3i}+{134×i=03i+i=013%4i}+{135×i=04i+i=013%5i}...= \{\lfloor\frac{13}{1}\rfloor × \sum_{i=0}^{0}i\}\\ + \{\lfloor\frac{13}{2}\rfloor × \sum_{i=0}^{1}i + \sum_{i=0}^{13\%2}i\}\\ + \{\lfloor\frac{13}{3}\rfloor × \sum_{i=0}^{2}i + \sum_{i=0}^{13\%3}i\}\\ + \{\lfloor\frac{13}{4}\rfloor × \sum_{i=0}^{3}i + \sum_{i=0}^{13\%4}i\}\\ + \{\lfloor\frac{13}{5}\rfloor × \sum_{i=0}^{4}i + \sum_{i=0}^{13\%5}i\}\\ ...

=j=113{13j×i=0j1i+i=013%ji}= \sum_{j=1}^{13}\{\lfloor\frac{13}{j}\rfloor × \sum_{i=0}^{j-1}i + \sum_{i=0}^{13\%j}i\}

EVN=j=1N( Nj×i=0j1i) N2+j=1N( i=0N%ji) N2EV_N = \dfrac{\sum_{j=1}^{N}(\ \lfloor\frac{N}{j}\rfloor × \sum_{i=0}^{j-1}i )\ }{N^2} + \dfrac{\sum_{j=1}^{N}(\ \sum_{i=0}^{N\%j}i )\ }{N^2}

Therefore, for any given M, we have to calculate:

N=1M(j=1N( Nj×i=0j1i) N2+j=1N( i=0N%ji) N2)\sum_{N=1}^{M}(\dfrac{\sum_{j=1}^{N}(\ \lfloor\frac{N}{j}\rfloor × \sum_{i=0}^{j-1}i )\ }{N^2} + \dfrac{\sum_{j=1}^{N}(\ \sum_{i=0}^{N\%j}i )\ }{N^2})

There are almost M×MM×M number of terms each of which we can calculate in constant time. So the complexity would be O(M×M)\mathcal{O}(M×M).

But we can optimize further. To do that, we have find the sub sequences of this series that contain same numerators or denominators. Then we can calculate each of those sub-sequences in constant time.

Let's optimize the first part first.

N=1Mj=1N( Nj×i=0j1i) N2\sum_{N=1}^{M}\dfrac{\sum_{j=1}^{N}(\ \lfloor\frac{N}{j}\rfloor × \sum_{i=0}^{j-1}i )\ }{N^2}

=x=1MN=1MNx×i=0x1iN2= \sum_{x=1}^{M}\sum_{N=1}^{M} \dfrac{\lfloor\frac{N}{x}\rfloor × \sum_{i=0}^{x-1}i }{N^2}

=x=1M( i=0x1i×N=1MNxN2)= \sum_{x=1}^{M}(\ \sum_{i=0}^{x-1}i × \sum_{N=1}^{M} \dfrac{\lfloor\frac{N}{x}\rfloor}{N^2} )

Now, let's focus on N=1MNxN2\sum_{N=1}^{M} \dfrac{\lfloor\frac{N}{x}\rfloor}{N^2}.

=( 012+...+0(x1) 2) +( 1x2+...+1(2×x1) 2) +...+( Mx1(Mx×xx)2+...+Mx1(Mx×x1) 2) +( Mx(Mx×x)2+...+MxM2)= (\ \frac{0}{1^2} + ... + \frac{0}{(x-1)\ ^2} )\ + (\ \frac{1}{x^2} + ... + \frac{1}{(2×x-1)\ ^2} )\ + ... + (\ \dfrac{\lfloor\frac{M}{x}\rfloor - 1}{(\lfloor\frac{M}{x}\rfloor×x - x)^2} + ... + \dfrac{\lfloor\frac{M}{x}\rfloor - 1}{(\lfloor\frac{M}{x}\rfloor×x -1)\ ^2} )\ + (\ \dfrac{\lfloor\frac{M}{x}\rfloor}{(\lfloor\frac{M}{x}\rfloor×x)^2} + ... + \dfrac{\lfloor\frac{M}{x}\rfloor}{M^2} )

We can calculate this sum in M/xM/x complexity if we precalculate cumulative sum of 1i2\dfrac{1}{i^2} from 1 to MM.

So for x=1 to Mx = 1 \space \text{to} \space M, total complexity will be near M log(M)M \space \text{log}(M).

Now, for the second part:

N=1Mj=1N( i=0N%ji) N2\sum_{N=1}^{M}\dfrac{\sum_{j=1}^{N}(\ \sum_{i=0}^{N\%j}i )\ }{N^2}

=N=1Mj=1N( ( N%j) ( N%j+1) 2) N2= \sum_{N=1}^{M}\dfrac{\sum_{j=1}^{N}(\ \frac{(\ N\%j)\ (\ N\%j + 1)\ }{2} )\ }{N^2}

=N=1Mj=1N( N%j) ( N%j+1) 2×N2= \sum_{N=1}^{M}\dfrac{\sum_{j=1}^{N}(\ N\%j)\ (\ N\%j + 1)\ }{2 × N^2}

=x=1MN=1M( N%x) ( N%x+1) 2×N2= \sum_{x=1}^{M} \sum_{N=1}^{M}\dfrac{(\ N\%x)\ (\ N\%x + 1)\ }{2 × N^2}

Now, let's focus on N=1M( N%x) ( N%x+1) 2×N2 \sum_{N=1}^{M}\dfrac{(\ N\%x)\ (\ N\%x + 1)\ }{2 × N^2} .

Let N=k×x+jN = k×x + j. Therefore N%x=jN\%x = j.

=kjj(j+1)2×(kx+j)2= \sum_{k}\sum_{j} \dfrac{j(j+1)}{2 × (kx +j)^2}

=kj12×{1+12kxkx+j+(kx)2kx(kx+j)2}= \sum_{k}\sum_{j} \frac{1}{2} × \{ 1 + \dfrac{1 - 2kx}{kx + j} + \dfrac{(kx)^2 - kx}{(kx+j)^2} \}

The summation is modified in a way so that there are no variable jj remains in the numerator. Now we can calculate the inner sum in constant time. So, once again, the total complexity reduced to M log(M)M \space \text{log}(M).

Statistics

73% Solution Ratio
tmwilliamlin168Earliest, Feb '20
nusuBotFastest, 0.1s
sajib_readdLightest, 8.1 MB
Kuddus.6068Shortest, 787B
Toph uses cookies. By continuing you agree to our Cookie Policy.