It's matter of practice and using the right algorithms. With practice there is a short term effect that will make you better at this over a period of weeks to months. But I've noticed that I have continued to improve on much longer time scales. I can do much more complex computations in my head than I could do when I was 20 years old.
Let's start with the right algorithms. In the division example you mentioned, I think you are using long division, but you should use synthetic division. I can easily solve the division problem in my head, because I do the division as follows. I first divide the polynomial by $x$, this yields:
$$x^2 - 5 x + 3$$
where I've dropped the fractional part. Then we just add to the coefficient of $x$ the coefficient of $x^2$. If we were dividing by $x-a$, we would add $a$ times the coefficient of $x^2$ to the coefficient of $x$. So, we then get the intermediary result:
$$x^2 - 4 x + 3$$
And then we need to add the coefficient of $x$ which is $-4$ to the constant term of $3$, this gets us to the result:
$$x^2 - 4 x - 1$$
Usually there are many different methods to do computations, the methods that are taught are usually what's most convenient for working things out on paper, not for mental computations.
Lifestyle and diet
Another important factor that's usually ignored in these sorts of questions is lifestyle and diet. When I was 20 I was not eating the sort of extremely healthy diet I'm eating now. I now eat a mainly whole food, plant-based diet. I do eat meat and fish, but in small quantities. This sort of a diet gives me much more fiber than a normal diet, and it's now known that fiber is important for the brain by feeding the microbiome, see here. I get 100 grams of fiber a day, while most people get less than 20. I also get 150 grams of protein a day while the RDA for me would be just 60 grams and would on a normal diet be met by meat and dairy. About 100 grams of that protein comes from only the dry bread, vegetables, pasta, potatoes I eat.
Changing my diet happened gradually over the years. It's not possible to change a normal diet to the sort of diet I'm eating in just a few weeks, as your intestines won't be able to process the sheer volume of food and the massive amount of fiber in it.
I also exercise a lot, about 1 hour of fast running per day. This burns about 1000 Kcal and therefore need to eat 1000 kcal more. All that extra food contains a massive amount of nutrients that will also benefit my brain.
Another thing I do that most people don't do is that I take a large dose of vitamin D. I take 10,000 IU on 5 days per week, so my intake is about 7,000 IU/day. This is much higher than the RDA, but it's what I would get from the Sun if I were outside all day long in a country at a latitude consistent with my skin color.
Now, many brilliant mathematicians who are very good at working out things mentally do not stick to a healthy lifestyle. Here genetics may play a role. The human body is extremely robust, it can function quite well on a suboptimal diet. But on the long term the body will have to compensate for that, it will adjust gene expression and switch to an alternative mode of operation. The consequences for that for your energy levels and your long term health will then be dependent on your genetics.
So, there are people with the right genes whose academic performance is not affected all that much by a suboptimal lifestyle, they end up becoming the new professors while the rest of the population will end up not pursuing an academic career.
Sleep
Getting enough and good quality sleep is obviously also important. One thing I noticed is that when I was about 20 years old, I could not read while dreaming. So, in a dream, I could see letters but I was unable to read. But this gradually changed. I now do dream about seeing texts and being able to read. This may be important for the brain to develop the ability to efficiently process texts and formulas mentally.