In problem 10055 at the UVa online judge, we are asked to help Hashmat the brave warrior to decide whether he should fight against his opponents. We do that by calculating the difference between the number of Hashmat’s soldiers and the number of his opponent’s soldiers.

## Interpretation

Hashmat tells us the number of soldiers in his army, and the number of soldiers in the opponent’s army. We don’t know in what order they are given, but we do know that Hashmat’s soldier number is never greater than his opponent’s soldier number. We need to calculate the difference between his soldier number and his opponent’s soldier number. We also know that neither soldier number is greater than , but that is a big number, so we’re going to store them in a *long* integer to be on the safe side. Using a normal 32-bit integer could cause overflow problems and lead to an incorrect solution.

## Implementation

We’re going to read each pair of numbers. We need to figure out which number is Hashmat’s army, and which number is his opponent’s army. That can easily be done since we know that Hasmat’s army is always the smaller number. Then we print out the difference between the numbers, which is the opponent’s number minus Hasmat’s number.

Scanner in = new Scanner(System.in); PrintWriter out = new PrintWriter(System.out, true); while (in.hasNextLong()) { long a = in.nextLong(), b = in.nextLong(); long hashmat, opposite; if (a <= b) { hashmat = a; opposite = b; } else { hashmat = b; opposite = a; } out.println(opposite - hashmat); }

This is actually a lot of code for such a simple problem. Let’s try to make it more compact.

We can note that the difference is always positive, since Hashmat’s soldier number is never greater than his opponent’s soldier number. If we calculate Hashmat’s number minus the opponent’s number, instead of the opposite, then we will get the negative of the difference we’re after. So in either case the absolute value (the number without the sign) of , where is either Hashmat’s or his opponent’s soldier number and is the opposite, will be the difference we’re after.

The code in the *while*-loop can now be written as follows.

long a = in.nextLong(), b = in.nextLong(); out.println(Math.abs(a - b));

## Conclusion

With only a couple of lines of code we helped keep Hashmat, the brave warrior, and his soldiers alive. The problem was not a hard one, but many people have had issues with overflow, as well as misinterpreting the problem description.

The complete source code is located here. Leave a comment if you have any questions, or if you want to tell us how you solved the problem.

Why are you solving such an easy problems? You should solve more PE instead.

Kristian handles the Project Euler problems, while I write about UVa problems. You need to talk to him if you want more PE

But the first reason for doing such an easy problem is that I’ve started going through the “Next Problems to Solve” list on uHunt. It is loosely sorted by increasing difficulty, so the first problems are easy. But they’ll get hard soon enough.

The second reason for doing an easy problem is that everybody needs to start with the basics. A newcomer can start by reading these easier posts before advancing to the more harder posts/problems.

If you have a good reason for me not writing about these easier problems, then please let me know. Otherwise I will keep on going, and over time get to the harder problems.

What is easy for some might not be it for others. I tend to agree with you that this is an easy problem.

The typical reader of these problems will be people who are new to UVa. They are not searching for a solution strategy since that is fairly straight forward, but for some reason they are stuck on a problem related to the input formatting or the like.

Besides that it apparently helps to get people like you involved in the discussion. A side bonus I didn’t see comming.

/Kristian

ok, so I’ll do Uva also, as a training in C

That’s a great idea! Take a look at the UVa introduction if you need help with the UVa judge. You can also take a look at our UVa solutions.

No need to introduce me to Uva I am registrated there, but i didn’t know about uHund. From my previous post I’ve already solved 3 Uva problems. Good luck

#include

int main()

{

long long int x,a,b;

while(scanf(“%lld %lld”,&a,&b)==2)

{

if(a>b)

x=a-b;

else

x=b-a;

printf(“%lld\n”,x);

}

return 0;

}

why while(scanf(“%lld%lld”,&a,&b)==2) is used in hashmat solution?

afrida: The scanf function returns the number of items that it was able to read. So when it reads two integers, it returns 2. But when there are no more integers to be read, it won’t return 2 (instead, it will return EOF). So this line in raj’s solution is basically reading in each test case, one after another, until there are no more test cases.

how to reduce run time in this problem. with this answer it takes 0.064s.

hoimonty: The fastest I can get it down to is 0.040s with the following C code:

The readn function is a modified version of the readn function found here.

In this problem, the bottleneck is reading the input, since apart from that you are almost not doing anything. So if you want a faster solution, you need to optimize the input reading (and possibly also the output writing).

Oh wow, I just noticed that there are 25635 users that have solved the problem, and 0.040s puts me in 31th place. It probably wont get much better than that…

In case anyone is interested, I got the running time down to 0.032s (which puts me in 23rd place) by optimizing the output. Now I convert the integers to a string myself, and also store all output in a buffer which I output only once at the end with fwrite. I also tried reading all the input at once into a buffer, but that was slower for whatever reason.

Usually I’m more for the high-level optimization, but in this instance I found it was quite fun to get down and dirty and do some low-level optimization.

Thanx 2 Bjarki Ágúst.

It will help me 2 know more.

why take long long int i knew that int size is 32 bit so pow(2,32) is fits to the int