DarkZero's blog

By DarkZero, 11 years ago, In English

In CodeForces #243 Div 2 B (something to do with mirroring), I submitted a solution (number is 6543367), and on the 10th test it gives 2 with GNU C++ 4.7 compiler, but on my own computer (Windows 8, gdb 6.8) it gives the correct answer 1.

And then I tried to switch to Microsoft VC++ 2010, and got AC.....

I checked my code twice and did not discover any hazard parts. Is there anyone could explain why? Thanks!

  • Vote: I like it
  • 0
  • Vote: I do not like it

»
11 years ago, # |
  Vote: I like it +2 Vote: I do not like it

I've edited your solution a bit. 6543599

Problem in

double tmp = log((double)(n/x)) / log(2.0);
	if( tmp != int(tmp))
		return false;
  • »
    »
    11 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Thank you very much for your attention! Does the solution imply that I should avoid using "log" when compiling with GNU C++?

    • »
      »
      »
      11 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Code that looks like:

      double a;
      if(a != int(a))
      

      usually bad idea (imho).

      Look at this 6543990

      • »
        »
        »
        »
        11 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Thank you, I will use

        double tmp;
        if( tmp != round(tmp) )
        

        instead.

        Learnt a lesson today, thank you!

        • »
          »
          »
          »
          »
          11 years ago, # ^ |
            Vote: I like it +31 Vote: I do not like it

          It's also a bad idea. You should avoid using operators == and != together with floating points variables. For example, instead of the code you've provided in the last comment one can write the following:

          double a;
          if (abs(a - round(a)) > eps) ...
          

          Here eps should be small enough, e.g. 10 - 9. Actually, its value depends on the problem you are solving.

          • »
            »
            »
            »
            »
            »
            11 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Thank you, I have known this trick but in that round I was too lazy to write in that way, and it came back to me = =

  • »
    »
    11 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    (Deleted)