PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#GL0000282. Số học modulo (Modular Arithmetic)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Số học modulo (Modular Arithmetic)
Modular Arithmetic
Source: Kattis
Version: Phuoc Hung OJ Extended
Problem Statement
Given a modulus and operations in order, evaluate each operation modulo , where is one of +, -, *, /. Division means multiplying by the multiplicative inverse of modulo ; if such an inverse does not exist, print -1. All valid residues are between and .
Input
- The first line contains integers and .
- Each of the next lines contains integers separated by an operator .
Output
Print lines, each containing the result of the corresponding operation, or -1 if the modular division is undefined.
Subtasks
- Subtask 1 (20 points): ; ; ; is one of
+,-,*,/. - Subtask 2 (30 points): ; ; ; is one of
+,-,*,/. - Subtask 3 (50 points): ; ; ; is one of
+,-,*,/.
Examples
Input
7 4
3 + 6
3 - 6
3 * 6
3 / 2
Output
2
4
4
5
Explanation
Addition yields . Subtraction yields . Multiplication yields . Since the inverse of modulo is , division yields .