09 March 2018

UVA 11876 - N + NOD (N) (Explanation)

Problem Explanation

Problem Category : Medium
Algorithm: Binary Search, Mathematical Simulation

For this problem, if you know how to find out total divisor of a number in a efficient way, then your job 85% is done. For this problem at first I have found out all divisor of a number and added with that number.
That is if the number is x then x = x + divisor(x)
and also marked those x's by using an array and also found out total number of numbers in the sequence for the range 1 to 1000000. In a test case I just calculate the difference between the ranges.

If you want to use binary search algorithm the you need not sum up the array. For using binary search algorithm just store all x's in a array and run a loop from a to b and search the value and count.

One important thing. If you use array to mark the x's then you can declare the array in the main function or globally. But don't forget to set 0 to all indices of the array if you declare the array in the main function. If you declare the array globally then you need not set 0 to all indices. Now it's your choice what should to do for less time complexity 

For source code Click


No comments:

Post a Comment

UVA 10679 - I Love Strings!!