Modular Arithmetic

Practice this coding question on Coding Gurukul.

Problem statement

A circular museum display has m numbered positions, from 0 to m - 1. Its motor starts at position 0 and moves forward by a positions on every button press, wrapping around the circle. A curator wants to align the display with position b and compare all possible press counts below m. Your task For each display, find the smallest integer x in the range 0 <= x < m for which a*x has remainder b upon division by m. Also count how many integers in that range satisfy the condition. Important details Zero presses are allowed. A motor with a = 0 never leaves position 0. If the target cannot be reached, report -1 0. The modulus need not be prime, so a modular inverse may not exist. Calculate products with 64-bit integers. Displays are independent; reset to position 0 before each one. Worked example For a = 6, b = 8 and m = 14, six presses give 36 mod 14 = 8. Thirteen presses also work, and no other count from 0 through 13 does. Print 6 2. With a = 4, b = 3 and m = 6, the display only visits even positions, so print -1 0. With a = 0, b = 0 and m = 5, all five allowed counts work, so print 0 5.

Input format

First line: t. Each of the next t lines contains a b m.

Output format

For each case print the smallest valid x and the number of valid x values, or -1 0 if none exist.

Constraints

1 <= t <= 100; 1 <= m <= 1000000000; 0 <= a <= 1000000000; 0 <= b < m.

Sample input

3
6 8 14
4 3 6
0 0 5

Sample output

6 2
-1 0
0 5

Sign in to run or submit a solution in C++, Java or Python.

Loading Coding Gurukul…