Блог пользователя Atomsky

Автор Atomsky, история, 4 года назад, По-английски

In CF Round #727 (Div. 2), I submitted 120147862 for Problem-C: Stable Groups. It gave a wrong result on test 62 in GNU C++17. When I submitted the same code in GNU C++11, it was accepted (120149347).

I tried to figure out what went wrong in C++17. If I simply add a line that prints out the array output after line 14, like cout << foo[i] << endl;, the code gives the correct result in C++17. I am having trouble understanding why this might happen! What can I do in such cases?

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
4 года назад, # |
  Проголосовать: нравится +11 Проголосовать: не нравится

It's probably the fact that you are using doubles, and (I heard that) doubles in C++11 and C++17 work differently

instead of

ceil((jar[i] - jar[i-1])/(double)x);

you can try

(jar[i] - jar[i - 1] + (x - 1)) / x;

this will avoid working with doubles, as ceil(a / b) can be equivalent to (a + b - 1) / b

»
4 года назад, # |
Rev. 2   Проголосовать: нравится +25 Проголосовать: не нравится

Dont use ceil because 2+eps might be ceilied to 3. As a rule of thumb avoid floats when you can in CP. It is not UB, mingw 32bit is configured to use excess precision by default it means that it will do all the operations in 80 bit float or sth like that. 64 bit one isn't it seems.

»
4 года назад, # |
Rev. 2   Проголосовать: нравится +31 Проголосовать: не нравится

I have fallen into this trap at least twice. For a rational number $$$r=\frac pq$$$, when you want to calculate $$$\left\lceil\frac pq\right\rceil$$$, don't use ceil(1.0 * p / q) (which can cause serious accuracy errors when $$$p$$$ and $$$q$$$ are very large), but use (p + q - 1) / q instead. Note that some changes need to be made when $$$p,q$$$ is negative, because in C++ signed integer division truncates towards zero. You can read this post for more information.