# UVa 138

## 138 - Street Numbers[edit]

## Summary[edit]

Find the numbers for a person's house and a block assuming that houses are consecutively numbered. The house is the starting point so the difference between the sum of all houses between the first and the person's house and the sum of all houses after the person's house until the block length is 0.

## Explanation[edit]

- This is impossible to solve within the time limit if you use a very naive O(n^3) algorithm which for each number between 1 and INTEGER_MAX find, a divisor such that sum of (house 1 to person's house-1) = sum (person's house+1 to end of block). This took a really long time I didnt have any luck getting past the fourth answer with this implementation!
- Another approach is to minimize the number of operations so we can get this very close to n. This approach requires keeping track of the difference between the first partition and the second partition and manipulating the divider of the partition properly. So for each number between 1 and INTEGER_MAX manipulate calculate the difference of adding the new integer. Manipulate the divisor until the difference is not positive.
- Remember not to include the dividing house.
- Remember you have to end the program when you reach the 10th element!
- The formula for computing this is 2N^2 = X^2 + X, where N is the low number in the pair and X is the high one. (2(6^2) = 8^2 + 8)

Note by ocin_lr: This result comes from the fact that:

Now, applying Gauss Formula:

This equation, however, is not very useful if you want a computer to compute it - at least in Java a double variable is overflown because of square of x. However when I ran the program I got first 5 pairs before it overflows, from which it's easy to spot a pattern in the numbers. The first number of all pairs is always

where is the first number of the preceding pair and is the first number of a pair before the preceding. So the first numbers of first 5 pairs are computed as follow:

and so on. Like this you can compute all first numbers of the ten pairs without any effort.

The computation of the second number of any pair is similarly easy:

To compute the *i*th second number of a pair we add up the first number of the current pair with both first and second numbers of the preceding pair. Thus, the first five numbers again are:

and so on.

**Alternatively**, brute force attack can be applied.

From the equation

we get,

So, we'll check every integer (from 8) as the value of for which gets an integer value.

## Optimizations[edit]

- Get the output from the program and just format it accordingly and display it. ACM judge wont know the difference.

## Output[edit]

6 8 35 49 204 288

## Solution[edit]

- Java: http://acm-solution.blogspot.com/2010/11/acm-uva-138-street-numbers-java.html
- C++:

- (brute force) : http://github.com/shahed-shd/Online-Judge-Solutions/blob/master/UVa-Online-Judge/138%20-%20Street%20Numbers.cpp
- http://acm-solution.blogspot.com/2010/10/acm-uva-138-street-numbers.html
- http://shipuahamed.blogspot.com/2013/02/problem-link-solution-uva-problem-no.html
- Code All the Problems