Recently I came to know that the mod(\'%\') operator is very slow. So I made a function which will work just like a%b. But is it faster than the mod operator?
Just contributing a little bit with this discussion. If you want to handle negative numbers, use the following function:
inline long long mod(const long long x, const long long y) {
if (x >= y) {
return x % y;
} else if (x < 0) {
return (x % y + y) % y;
} else {
return x;
}
}