Mackenzie New Year's Challenge P1 - King Modulus

View as PDF

Submit solution

Points: 3 (partial)
Time limit: 2.0s
Memory limit: 256M

Problem type
Allowed languages
Ada, Assembly, Awk, Brain****, C, C#, C++, COBOL, CommonLisp, D, Dart, F#, Forth, Fortran, Go, Groovy, Haskell, Intercal, Java, JS, Kotlin, Lisp, Lua, Nim, ObjC, OCaml, Octave, Pascal, Perl, PHP, Pike, Prolog, Python, Racket, Ruby, Rust, Scala, Scheme, Sed, Swift, TCL, Text, Turing, VB, Zig

Atharva hates modulo (long story). Due to this hate, he learned everything possible about modulo and has been self-named King Modulus. He wants you to share his frustration of modulo by giving you a task. Given two integers N and M, find the value of N \bmod M. The answer for N \bmod M can be viewed as the remainder of a division, where N is the dividend and M is the divisor. The result of a modulo operation is always non-negative. Here is an example of how the modulo operation works:

\displaystyle N \bmod M = (N - M) \bmod M = (N - 2M) \bmod M = \ldots = (N + M) \bmod M = (N + 2M) \bmod M = \ldots

For example, 7 \bmod 4 is:

\displaystyle 7 \bmod 4 = 3 \bmod 4 = -1 \bmod 4 = -5 \bmod 4 = 11 \bmod 4 = 15 \bmod 4 = 3

Input Specification

A single line containing N and M separated by a single space.
-10^9 \le N \le 10^9
1 \le M \le 10^6

Output Specification

One line containing the result of N \bmod M.

Sample Input 1

7 4

Sample Output 1


Sample Input 2

15 3

Sample Output 2



  • -2
    WzRd  commented on Dec. 17, 2017, 7:54 p.m.

    Im having trouble with test case 6?