This is a simple formula.
A ^ x % c = a ^ (x % phi (c) + phi (c) % c where x> = phi (c)
Phi (c) is the Euler's function.
Note that the Euler function is defined as the number of positive integers not greater than n and with n, instead of less than n.
And phi (1) = 1
This question is divided into three parts:
Part 1 n! <Phi (c) Then you need to calculate n directly! Fortunately, phi (c) is not very large.
Part 2 n! > = Phi (c) but n! % Phi (c )! = 0 this part also requires brute-force computation, but the modulo operation can be performed without a very large number.
Part 3 n! > = Phi (c) and n! % Phi (c) = 0 is converted to n ^ phi (c) % c and then (n % c) ^ phi (c) % c is a loop section with the length of c.
In the end, it is important to note that the question has a large scope and requires uLL, and the answer may exceed uLL, because when c = 1, B = 0, 0 to 2 ^ 64-1 are all qualified, so special judgment is required.
The code is not given, and it is not difficult to write.
Author: sdj222555