Remainder Sequence Cycle
InterviewTime limit1sMemory limit128 MB
Given N and P, simulate the sequence a_{i+1} = a_i*N mod P and output the length of the eventual repeating cycle.
- Level
Easy3 of 10
- Topics
- Simulation, Hash map, Math
- Solved
- No attempts yet
Problem
Given two natural numbers N and P, define a sequence a_0, a_1, a_2, ... as follows.
a_0 = N- For
i >= 0,a_{i+1}is the remainder whena_i * Nis divided byP.
If this process continues, a value that has already appeared will appear again, and from then on the same values repeat in the same order. Write a program that finds how many distinct values are contained in the repeating part.
Input
The first line contains the two starting natural numbers N and P, separated by a space.
Output
Print the number of distinct values contained in the repeating part.
Constraints
1 <= N <= 1,0002 <= P <= 97