Algorithm of 11526 - H(n)

Problem Description Link
Algorithm
The problem is not difficult but we need to consider the problem of time limit exceeded, e.g. we are computing n/n, n/n-1, .....there are so many ones and twos,... these can be computed in one step. We can observe the number of 1, 2, 3.....the number of j is n/j - n/(j+1).  so when the number is small and many, we use the n/j - n/(j+1) method. for numbers big and few, just use the original method. We can set the break point at sqrt(n). Do not repeat the values around sqrt(n). Try to avoid division by 0 and sqrt of negative numbers.

Example:
if n=5 series is 5/1 + 5/2 + 5/3 + 5/4 + 5/5
    then sqrt(5)=2 need loop two times
    5/1-5/2=3 means 1 get 3 times, so SUM=3*1=3
    5/2-5/3=1 means 2 get 1 times, so SUM=3+2*1=5
    so last 4 (3+1) steps complete from the series ,so now we nedd calculate first 1 step
    5/1=5 , so SUM=5+5=10
if n=10 series is 10/1 + 10/2 + 10/3 + 10/4 + 10/5+.... +10/10
    then sqrt(10)=3 need loop two times
    10/1-10/2=5 means 1 get 5 times, so SUM=5*1=5
    10/2-10/3=2 means 2 get 2 times, so SUM=5+2*2=9
    10/3-10/4=1 means 3 get 1 time, so SUM=9+3*1=12
    so last 8(5+2+1) steps complete from the series ,so now we nedd calculate first 2 step

    10/1=10 , so SUM=12+10=22
    10/2=5 , so SUM=22+5=27
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience