We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • HackerRank Home
  • |
  • Prepare
  • Certify
  • Compete
  • Apply
  • Hiring developers?
  1. Prepare
  2. Mathematics
  3. Number Theory
  4. Period

Period

Problem
Submissions
Leaderboard
Discussions
Editorial

You are given 2 integers a and b. Let a number be defined as image1. As we know image1 will be an irrational number when b is non-zero. In this problem, we call it the AC number. We define

Image2 (where x an integer)

and the operation Image3 on AC number as:

Image4

This problem is to find the smallest positive integer n, such that:

Image5

We call the integer n as period. You are given a, b and m. Can you figure out the period?

Input Format
The first line of the input contains a single integer T denoting the number of test-cases.
T lines follow, each containing 3 integers - a, b and m separated by a single space.

Output Format
Output the Period if it exists, otherwise output "-1" (quotes only for reference)

Constraints
1 ≤ T ≤ 300
5 ≤ m ≤ 107
0 ≤ a, b < m

Sample Input #00

4
0 0 13
1 0 7
3 0 10007
1 1 19

Sample Output #00

-1
1
5003
18

Explanation #00

For the 1st test-case, no amount of operation ⊗ on a = 0, b = 0 gives 1 on the RHS. Hence the answer is -1.
When a = 1, b = 0, we have 1 for n = 1.
On repeated operations, the third and the fourth testcases sum to 1 for n = 5003 and n = 18 respectively.

Author

dheeraj

Difficulty

Hard

Max Score

100

Submitted By

397

Need Help?


View discussions
View editorial
View top submissions

rate this challenge

MORE DETAILS

Download problem statement
Download sample test cases
Suggest Edits
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Helpdesk
  • Careers
  • Terms Of Service
  • Privacy Policy

Cookie support is required to access HackerRank

Seems like cookies are disabled on this browser, please enable them to open this website