ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1068. Sum

I submitted 1068 twice and I always receive "WA"
Then I read about the formula and I compared the
answers(as you can see down) and they were all
the same. Then I submitted the task once again
with the the formula and I got "AC". Can someone
explain why?

var i,sum1,n,sum:longint;
begin
for n:=-10000 to 10000 do
begin
   {formula}
   if n < 1 then sum := (( 1 + n) * ( 2 - n)) div 2
   else  if n >=1 then sum := (( n + 1) * n) div 2;

   {my solution}
   sum1:=0;
   if n=1 then sum1:=1
   else
     if n<=0 then
       for i:=1 downto n do sum1:=sum1+i
     else
       for i:=1 to n do sum1:=sum1+i;

   {if there were different answers}
   if sum<>sum1 then writeln(n,' ',sum1,' ',sum1); readln;
end;

end.
Dinh Quang Hiep (mg9h@yahoo.com) Maybe you got WA with your solution because of your output format [4] // Problem 1068. Sum 28 May 2001 11:50
> I submitted 1068 twice and I always receive "WA"
> Then I read about the formula and I compared the
> answers(as you can see down) and they were all
> the same. Then I submitted the task once again
> with the the formula and I got "AC". Can someone
> explain why?
>
> var i,sum1,n,sum:longint;
> begin
> for n:=-10000 to 10000 do
> begin
>    {formula}
>    if n < 1 then sum := (( 1 + n) * ( 2 - n)) div 2
>    else  if n >=1 then sum := (( n + 1) * n) div 2;
>
>    {my solution}
>    sum1:=0;
>    if n=1 then sum1:=1
>    else
>      if n<=0 then
>        for i:=1 downto n do sum1:=sum1+i
>      else
>        for i:=1 to n do sum1:=sum1+i;
>
>    {if there were different answers}
>    if sum<>sum1 then writeln(n,' ',sum1,' ',sum1); readln;
> end;
>
> end.
>
Ivo What do you mean??? [3] // Problem 1068. Sum 29 May 2001 00:07
This is not my output this is a program that compares the
two methods of finding the solution
   if sum<>sum1 then writeln(n,' ',sum1,' ',sum1);
Dinh Quang Hiep (mg9h@yahoo.com) i know, but i think your own program have some mistake with the output [2] // Problem 1068. Sum 29 May 2001 10:52
Ivo What mistake??? Tell me more... [1] // Problem 1068. Sum 30 May 2001 00:48
>
Dinh Quang Hiep (mg9h@yahoo.com) Tong Hop always Vo Dich, and the main reason belongs to ...... // Problem 1068. Sum 30 May 2001 03:04
the main reson belongs to the server. Maybe some thing wrong
with compiling
i've submit your solution

 var i,sum1,n,sum:longint;
 begin
    read(n);
    {my solution}
    sum1:=0;
    if n=1 then sum1:=1
    else
      if n<=0 then
        for i:=1 downto n do sum1:=sum1+i
      else
        for i:=1 to n do sum1:=sum1+i;

    writeln(sum1);

 end.

and got WA, but when i change it to

 var i,sum1,n,sum:longint;
 begin
    read(n);
    {my solution}
    sum1:=0;
    if n=1 then sum1:=1
    else
      if n<=0 then
            (changing in this line)
        for i:=n to 1 do sum1:=sum1+i

      else
        for i:=1 to n do sum1:=sum1+i;

    writeln(sum1);

 end.

And i got AC, i don't know why, but maybe the compiler
define that n is always >=1  and it does not run the command
"for i:=1 downto n", so u get WA. So that, don't use "downto
"when not necessary !

Btw, your program will run slower than any program that use
the simple formula.

QH@