Incorrect answer when using numpy.prod () for B problem of ABC169

** B problem ** https://atcoder.jp/contests/abc169/tasks/abc169_b

I read this problem and thought that if there was a function of the multiplication version of sum that multiplies everything, I thought that it could be done in one shot, and when I looked it up, numpy.prod () was that, so when I tried using it, it was WA.

WA https://atcoder.jp/contests/abc169/submissions/15692265

So, normally multiply by the for statement and AC. It seems that multiplication of large numbers will be slow, so I try to break as soon as the range is exceeded.

AC https://atcoder.jp/contests/abc169/submissions/15692483

I investigated numpy.prod.


Jupyter notebook for research is published on gist

https://gist.github.com/kokuyokugetter/0b3718a7b0bd0adf8f3c0f6f6e483b9e


Comparing the last two cells, you can see that the total product from 1 to 20 is the same, and numpy.prod () is different from 21.

It seems that numpy is implemented in C language and fortran, so the value obtained by dividing the maximum signed value of 64-bit integer 9223372036854775807 by the total product of 1 to 20 2432902008176640000 is about 3.8, so it is not possible to shift after 21. There is no sense of discomfort.

After that, even if the total product of 1 to 20 was multiplied by 4, it was misaligned, so it is probably correct that the maximum value is 9223372036854775807.

It seems to be more certain if you investigate how the bit changes and becomes negative, but in terms of competition pros, the total product ends with the feeling that you should do it seriously just by embedding python.

Recommended Posts

Incorrect answer when using numpy.prod () for B problem of ABC169
Trial to judge the timing of the progress display of the for loop using 0b1111 ~
3 types of workarounds for activate collision problem when pyenv and anaconda coexist
Impressions of using Flask for a month
[python, multiprocessing] Behavior for exceptions when using multiprocessing
Precautions when using for statements in pandas