10784 - Diagonal

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

Moderator: Board moderators

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

10784 - Diagonal

Post by sumankar » Sat Mar 12, 2005 11:33 am

I am trying this for ages now ...

Code: Select all

...
    long long int d, n;
...  
    double m;
...
/*  This is where I calculate the answer, ndia = n*(n-3)/2,
 ** frame a quadriatic, get the roots, take the +ve one */   
    m = (3.0 + sqrt( 9.0 + (d*8.0)))/2.0; 
   /* and output */
    printf(".0lf", m+0.5);

..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. » Sat Mar 12, 2005 1:53 pm

Precision error.
I used +0.5 as theshold when I need to round a "normal" floating point number(say, 3.4, 1.2, etc.) to integer.
If a floating point number is storing a number, which should be an integer in theory, I will use much smaller theshold like +1e-10. You will get AC by trying different combination of theshold value and ceil() or floor().
Last edited by .. on Sat Mar 12, 2005 2:42 pm, edited 1 time in total.
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.

neno_uci
Experienced poster
Posts: 104
Joined: Sat Jan 17, 2004 12:26 pm
Location: Cuba

Post by neno_uci » Sat Mar 12, 2005 2:09 pm

I solved this approximating the value of N via binary search..., i know this is not the best method but i am not so good at math but it worked ok..., :D

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Sat Mar 12, 2005 2:33 pm

Thanks, but
.. wrote:Precision error.
I used +0.5 as theshold when I need to round a "normal" floating point number(say, 3.4, 1.2, etc.) to integer.
If a floating point number is storing a number, which should be an integer in theory, I will use much smaller theshold like +1e10. You will get AC by trying different combination of theshold value and ceil() or floor().
+1e10 is small?

Code: Select all

#define EPS = 1e-8
m = (3.0 + sqrt( 9.0 + (d*8.0)))/2.0; 
        frac = modf(m, &whole);
        if ( frac < EPS )
            ;
        else
            whole++;
        printf("Case %d: %.0lf\n", k, whole);
doesn't work either.
Suman.

..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. » Sat Mar 12, 2005 2:42 pm

Sorry typo, should be 1e-10.
fmod() sucks, I never use it. And I already say using ceil or floor is enough.

The problem of your code is
printf(".0lf", m+0.5);
People think it will work perfectly in rounding, but it doesn't.
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Sat Mar 12, 2005 2:58 pm

.. - I resign.Can you give me how to do it?

I tried everything, in my capacity(rather limited, you see...:()

Suman

..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. » Sat Mar 12, 2005 3:05 pm

This is what I used in my AC code:

Code: Select all

 printf ("Case %d: %d\n", t++,          (int)ceil((3.0+sqrt(9.0+8.0*N))/2.0-1e-10));
Last edited by .. on Sat Mar 12, 2005 3:15 pm, edited 1 time in total.
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Sat Mar 12, 2005 3:11 pm

All of which gives a WA ... :lol:

I 'll go crazy.

Suman.

..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. » Sat Mar 12, 2005 3:18 pm

If the line of AC code can't give you AC, then you must have some other problems.

P.S. It seems that I don't remember the detail correctly, the second method in my last post is wrong.
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Sat Mar 12, 2005 3:22 pm

Code: Select all

scanf() == 1 && d 
was the culprit .

Here is the culprit, may be you can find something in here...
Suman.

..
A great helper
Posts: 454
Joined: Thu Oct 18, 2001 2:00 am
Location: Hong Kong

Post by .. » Sat Mar 12, 2005 3:24 pm

The mistake is obvious......
Run the sample input again........
My signature:
  • Please make discussion about the algorithm BRFORE posting source code.
    We can learn much more in discussion than reading source code.
  • I HATE testing account.
  • Don't send me source code for debug.

sumankar
A great helper
Posts: 286
Joined: Tue Mar 25, 2003 8:36 am
Location: calcutta
Contact:

Post by sumankar » Sat Mar 12, 2005 3:26 pm

..

Thanks a zillion tonnes ... I should have killed myself by now for that stupid^inf bug.

Suman.

User avatar
Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

WHY WA

Post by Debashis Maitra » Wed Dec 20, 2006 6:59 pm

WHY WA

Code: Select all


Removed after AC

}
Last edited by Debashis Maitra on Fri Dec 22, 2006 6:38 pm, edited 1 time in total.
Akash chhoyar swopno
Dream to touch the sky

Jan
Guru
Posts: 1334
Joined: Wed Jun 22, 2005 10:58 pm
Location: Dhaka, Bangladesh
Contact:

Post by Jan » Wed Dec 20, 2006 10:56 pm

Debashis, your code is correct, except two things.

1. Case ('C' capital).
2. The problem states...
Input is terminated by a line containing a zero
So, if you get a 0 then you can finish processing.

Hope you get accepted.
Ami ekhono shopno dekhi...
HomePage

User avatar
Debashis Maitra
Learning poster
Posts: 62
Joined: Sun Jul 09, 2006 8:31 am
Location: University of Dhaka
Contact:

Post by Debashis Maitra » Fri Dec 22, 2006 6:33 pm

Thanx Jan Bhai

that was a silly mistake
Akash chhoyar swopno
Dream to touch the sky

Post Reply

Return to “Volume 107 (10700-10799)”