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

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

I've been compiling locally with c++11, e.g. g++ -Wall -Wextra -Wpedantic -std=c++11 -o "D" "D.cpp", and I've been trying to figure out why I'm getting different results remotely. I submitted the same code to:

GNU C++11 (fails) https://mirror.codeforces.com/contest/1493/submission/110434447

GNU C++14 (passes) https://mirror.codeforces.com/contest/1493/submission/110479676

Locally my c++11 compilation is not getting the error shown in the submission on problem set 3. My gut feeling is that there's some out of bounds write occurring that i'm missing that just isn't affecting things by luck in the other submission, but if it is I just can't detect it. I would appreciate any insight (or critique of my code.)

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

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

Not sure if this helps, but pow() is inconsistent; see below

int main() {
	const long long x = 2039593823;
	cout << (long long)pow(x,2) << "\n";
	cout << x*x << "\n";
	// 4159942962819755520
	// 4159942962819755329
}
  • »
    »
    4 года назад, # ^ |
    Rev. 2   Проголосовать: нравится 0 Проголосовать: не нравится

    Yeah it is the pow function. When you use pow, you are dealing with floating points which are not accurate. You should write a function which doesn't involve floating points.

    Something like this

    Here is the link to the accepted submission.

    sqrt involves floating points as well. It's better to do f*f <= tmp

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

    Thank you! I'm a little surprised actually that the standard library pow doesn't have an overloaded case for just ints and long long ints to return a long long int, but it's great to know.

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

      I think the main reason the standard library doesn't have an overloaded case for ints or long longs is because it is easy to have overflow in those cases, but floating point multiplications are easy to scale to large powers.

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

        Yeah, but some kind of modpow() function would be nice, because it's used quite a bit, even outside of cp I think. iirc, Python has an optional mod in its pow() function.