I have come across programming questions where the output is requested as modulo 10^9+7
or some other prime number. My confusion is that why is the value returned by performing modulo operation considered as the correct answer. If for example 3!= 6
but 6%(10^9+7) = 13
, so why is 13
the correct answer when 3!
is actually 6
? When modulo changes the actual value why are we using it?
Asked
Active
Viewed 63 times
0

Shivam...
- 155
1 Answers
1
The correct answer is what the question asks for. Typically when problems ask for the answer modulo some number it is because the answer before doing the modulo operation is some very large number. Rather than have you enter a large string of digits the modulo reduces it to nine or ten (in the case of 10^9+7). It is considered large enough to prove you have solved the problem correctly.

Ross Millikan
- 374,822
-
So if the calculated value is large and we reduce it to some smaller value using mod, they compare our result against the mod value in test case or the actual large calculated value we got initially? Because to me the correct answer is the initial value. – Shivam... Apr 13 '23 at 04:03
-
Please strive not to post more (dupe) answers to dupes of FAQs, cf. recent site policy announcement here. – Bill Dubuque Apr 13 '23 at 04:24
-
2@FireMoon The correct answer is what the question asks for, which is the modulo value, not the larger value. Often on such a programming challenge it's necessary (for speed) to do many intermediate calculations mod $m$ too (instead of just doing the modulo operation once at the end) in order to avoid large intermediate results. The question authors might not even know the large value, because it might be too hard to compute (and/or too long to fit in any computer's memory). – Karl Apr 13 '23 at 04:42
6 % (10^9 + 7)
is6
, not13
, so I'm not sure what you're referring to. In any case, the modulo operator is a binary operator and, like most binary operators, in many (but not all) cases gives an output value that's different from either of the input values. (I'm assuming you're using the standard definition ofa % b
as the smallest nonnegative integer $n$ such that $a \equiv n \pmod{b}$. If you're not using this standard definition, please give the definition you're using.) – Daniel Hast Apr 13 '23 at 03:53