Page 1 of 3

### 10812 - Beat the Spread!

Posted: Mon Mar 21, 2005 2:02 am
Hello all,

I'm having a bit of a trouble with problem 10812: Beat the Spread!
Although the problem seems more than easy: x+y = s, x-y = d => ... => x = (s+d)/2 and y = (s-d)/2, finally check if both x and y a are non negative.

The problem is that I keep getting WA and I cannot figure why. Any sugestions?

./noddy

Posted: Mon Mar 21, 2005 2:09 am
You should check whether all these conditions hold: s >= d, (s + d) even, (s - d) even. When either fails, the answer is "impossible."

Posted: Mon Mar 21, 2005 2:44 am
mf wrote:You should check whether all these conditions hold: s >= d, (s + d) even, (s - d) even. Even either fails, the answer is "impossible."
Many thanks for your help. I forgot to check the even conditions.

./noddy

Posted: Mon Mar 21, 2005 6:05 pm
why do we have to check if s+d and s-d are even ?...i can't understand that...in the problem they don't talk about that...can someone xplain me??

### Re

Posted: Mon Mar 21, 2005 6:22 pm
Hi taborda,
Lets see an example :-

x=15,y=12
then x+y=27 (odd)
and x-y=3 (odd)
then if u do 27/2=13.5
and 3/2=1.5 all the result is fraction
now we see that there is no two integer(not fraction) which summation is X and difference is Y, thats why we check it for even. So for this input output should be "impossible". Hope it helps. Good Luck.

### 10812 So easy... =(

Posted: Thu Jun 09, 2005 11:48 pm
This problem is giving me some WA's and I dont know how to solve this. This problem is very easy, I know, but my code is wrong somewhere. I'll leave the code I'm using, here. I hope someone can give me an hint. This problem doesn't deserve so much time. Thanks...

Code: Select all

#include "stdio.h"

int main()
{
unsigned long int num, i, first, sec, x, y;

scanf("%lu", &num);

for(i=0; i<num;i++)
{
scanf("%lu", &first);
scanf("%lu", &sec);

x=(first+sec)/2;
y=(first-sec)/2;

if(first<=sec || (first+sec)%2 || (first-sec)%2 || x<0 || y<0)
{
printf("impossible\n");
continue;
}

printf("%lu %lu\n", y, x);
}
return 0;
}

Posted: Fri Jun 10, 2005 4:10 am

### Re: 10812 So easy... =(

Posted: Fri Jun 10, 2005 9:14 am
gateKeeper wrote:

Code: Select all

if(first<=sec || (first+sec)%2 || (first-sec)%2 || x<0 || y<0)
{
printf("impossible\n");
continue;
}

Ciao!!!

Claudio

### 10812...WA??

Posted: Sun Jun 11, 2006 5:38 pm
where is the fault in my code??
plz help.

Code: Select all

removed

### Re: 10812...WA??

Posted: Sun Jun 11, 2006 8:07 pm
asif_rahman0 wrote:where is the fault in my code??
plz help.
There is already a topic on this problem. See http://online-judge.uva.es/board/viewtopic.php?t=7782. Never create a new topic on a problem if there is already one on that problem.

Anyway, your solution assumes something that's not written in the problem statement. What if s=d? Read the problem description more carefully.

Posted: Sun Jun 11, 2006 8:31 pm
thnx martin:).

but just before u posted i got it accepted:D

sorry for posting it again as a new post.

Posted: Mon Dec 11, 2006 5:30 am
I still don't get it. I've tried all possible corrections, but none get AC.
I suppose that both the sum & difference must be either even or odd. I still get WA.
e.g.:

1. 15 12 (Odd & Even)
x + y = 15
x - y = 12
------------- +
x = (15 - 12) / 2
(doesn't fulfil)

2. 14 11 (Even & Odd)
x + y = 14
x - y = 11
------------- +
x = (14 - 11) / 2
(doesn't fulfil)

3. 15 11 (Odd & Odd)
x + y = 15
x - y = 11
------------- +
x = (15 - 11) / 2
(fulfils)

4. 14 10 (Even & Even)
x + y = 14
x - y = 10
------------- +
x = (14 - 10) / 2
(fulfils)

Here's my code:

Code: Select all

#include<stdio.h>

void main()
{
int i, s, d, n, a, c;
while(scanf("%d", &n)==1)
{
for(i=0; i<n; i++)
{
scanf("%d %d", &s, &d);
a = (s - d) / 2;
if((s%2==0 && d%2!=0) || (s%2!=0 && d%2==0) || (s-d)<0) printf("impossible\n");
else
{	if(a < s-a){ c = a; a = s - a; }
printf("%d %d\n", a, c);
}
}
}
}

Posted: Mon Dec 11, 2006 10:21 am
Suppose the input is 20 0. Now according to your code...

Code: Select all

scanf("%d %d", &s, &d); /* s = 20, d = 0 */
a = (s - d) / 2; /* So, a = 10 */
if((s%2==0 && d%2!=0) || (s%2!=0 && d%2==0) || (s-d)<0)
printf("impossible\n"); /* s and d both are even and s-d > 0 */
else
{   if(a < s-a){ c = a; a = s - a; } /* a = 10, s-a = 10, so a is not less than (s-a) */
printf("%d %d\n", a, c); /* Now c is not initialized */
}
The output should be 10 10. Hope it helps.

Posted: Tue Dec 12, 2006 6:27 pm
Got it! Thanx a lot, Jan

### Re: whats wrong??- Beat the Spread!

Posted: Fri Dec 26, 2008 11:01 pm
plz someone help me.
why wrong ans!!!
here my code.

Code: Select all

#include<stdio.h>

int main()

{  int s1,s2;

int  s,d;
int numinput,i;

scanf("%d",&numinput);

for(i=1;i<=numinput;i++)
{
scanf("%d %d",&s,&d);

if(d>=s){

printf("impossible\n");
}
else if
((s+d)%2!=0 || (s-d)%2!=0 || (s+d)<0 || (s-d)<0)

printf("impossible\n");

else
{   s1=((s+d)/2);
s2=s-s1;

printf("%d %d\n",s1,s2);
}

}
return 0;
}