Mohammed_Almasry's blog

By Mohammed_Almasry, history, 5 years ago, In English

I got Runtime error on test 17 in python and i don't know why 69797580 . I solved the problem (E. New Reform) in cpp with the same logic and passed 69725502 any help ?? Thanks

  • Vote: I like it
  • +2
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
Rev. 4   Vote: I like it +3 Vote: I do not like it

Python + Recursion = B A D (Reason ~ link).

Stack Overflow will happen in the case of a linear graph of 10^6 size. What can you do :

1.Use Any Other Language as you did already.(CPP ;-))

2.Use Threading and sys.setrecursionlimit(10**6) as I did (Modified your code and got AC).(69799972).

Example code to increase recursion limit and max stack size in Python.

code

Although it is not recommended since it gives MLE in many cases. Not mine

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

    Python + any kind of heavy object handling = bad, and a function call is also an object. Python + C modules for heavy computation = perfectly fine.

    Check out Python bindings for C libraries. When anything from a module implemented in C is used in Python, there are sooo many checks and conversions, everything is wrapped in a PyObject* and it's just way too costly. If you avoid these kinds of costly things, e.g. with Numpy array manipulation (=implemented in C) or JIT, working with large data can still be pretty fast.