Finding a billion factorials in 60 ms with SIMD

37 todsacerdoti 1 6/22/2025, 11:12:50 PM codeforces.com ↗

Comments (1)

few · 38m ago
It's interesting to see which codeforces blog posts get traction on HN.

For context, in competitive programming a lot of combinatorial problems (find some formula to count something) require you to output the answer modulo some prime. This is because otherwise the answer would overflow an int and make the problem too tedious to be fun and too hard for problem setters to write good problems or checkers for.

So to prove that you still know how to count the thing, you can do it a finite field. If you use integers mod prime, you still have all the usual arithmetic operations like addition subtraction multiplication. And even division (aka multiplicative inverse) is still easy since you can calculate it with Fermat's Little Theorem (a^(p-2) = a^(-1) mod p).

Anyway, just wanted to give context for why competitive programmers care about factorial mod a prime. And kind of still surprised anyone outside of it cares.