## 10812 - Beat the Spread!

All about problems in Volume 108. If there is a thread about your problem, please use it. If not, create one with its number in the subject.

Moderator: Board moderators

noddy693
New poster
Posts: 2
Joined: Mon Mar 21, 2005 1:56 am

### 10812 - Beat the Spread!

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?

Thanks in advance.

./noddy

mf
Guru
Posts: 1244
Joined: Mon Feb 28, 2005 4:51 am
Location: Zürich, Switzerland
Contact:
You should check whether all these conditions hold: s >= d, (s + d) even, (s - d) even. When either fails, the answer is "impossible."
Last edited by mf on Mon Mar 21, 2005 8:26 pm, edited 1 time in total.

noddy693
New poster
Posts: 2
Joined: Mon Mar 21, 2005 1:56 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

taborda
New poster
Posts: 6
Joined: Wed Mar 16, 2005 1:57 am
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??

Raj Ariyan
Learning poster
Posts: 70
Joined: Sat Feb 05, 2005 9:38 am
Location: Gurukul

### Re

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.
Some Love Stories Live Forever ....

gateKeeper
New poster
Posts: 2
Joined: Sat Jun 04, 2005 10:07 pm

### 10812 So easy... =(

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;
}

``````

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

CDiMa
Experienced poster
Posts: 214
Joined: Fri Oct 17, 2003 5:49 pm
Location: Genova

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

gateKeeper wrote:

Code: Select all

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

Ciao!!!

Claudio

asif_rahman0
Experienced poster
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm

### 10812...WA??

where is the fault in my code??
plz help.

Code: Select all

``````removed
``````
Last edited by asif_rahman0 on Sun Jun 11, 2006 8:32 pm, edited 1 time in total.

Martin Macko
A great helper
Posts: 481
Joined: Sun Jun 19, 2005 1:18 am
Location: European Union (Slovak Republic)

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

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.

asif_rahman0
Experienced poster
Posts: 209
Joined: Sun Jan 16, 2005 6:22 pm
thnx martin:).

but just before u posted i got it accepted:D

sorry for posting it again as a new post.

Steven Luck
New poster
Posts: 13
Joined: Sat Dec 02, 2006 7:51 am
Location: Indonesia
Contact:
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);
}
}
}
}
``````
"The Only Thing For The Triumph of Evil is For a Good Man to Do Nothing"

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:
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.
Ami ekhono shopno dekhi...
HomePage

Steven Luck
New poster
Posts: 13
Joined: Sat Dec 02, 2006 7:51 am
Location: Indonesia
Contact:
Got it! Thanx a lot, Jan
"The Only Thing For The Triumph of Evil is For a Good Man to Do Nothing"

sazzadcsedu
Experienced poster
Posts: 136
Joined: Sat Nov 29, 2008 8:01 am
Location: narayangong,bangladesh.
Contact:

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

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;
}
``````
Life is more complicated than algorithm.
http://felix-halim.net/uva/hunting.php?id=32359
For Hints: http://salimsazzad.wordpress.com