The explanation is too incisive and hard to explain...
Magic bull link http://www.cppblog.com/vontroy/archive/2010/10/02/128356.html
Calculate the factor of 2004 ^ X and S (2004 ^ X) mod m, M = 29
S (2004 ^ X) % 29
Factors and S are product functions, that is, gcd (a, B) = 1 => S (A * B) = S (a) * s (B)
2004 ^ x = 4 ^ x * 3 ^ x * 167 ^ x
S (2004 ^ X) = S (2 ^ (2x) * s (3 ^ X) * s (167 ^ X)
If P is a prime number => S (P ^ X) = 1 + P ^ 2 + + P ^ x = (P ^ (x + 1)-1) /(p-1)
S (2004 ^ X) = (2 ^ (2x + 1)-1) * (3 ^ (x + 1)-1) /2*(167 ^ (x + 1)-1)/166
167% 29 = 22
S (2004 ^ X) = (2 ^ (2x + 1)-1) * (3 ^ (x + 1)-1) /2*(22 ^ (x + 1)-1)/21
(A * B)/C % m = A % m * B % m * inv (c)
C * inv (c) = all the numbers where the 1% M modulo is 1 Inv (c) is the smallest and can be divisible by C
Inv (2) = 15, inv (21) = 18 2*15 = 1 mod 29, 18*21 = 1 mod 29
S (2004 ^ X) = (2 ^ (2x + 1)-1) * (3 ^ (x + 1)-1) /2*(22 ^ (x + 1)-1)/21
= (2 ^ (2x + 1)-1) * (3 ^ (x + 1)-1) * 15*(22 ^ (x + 1)-1) * 18
**************************************** ***********************/
# Include <iostream>
# Include <cmath>
Using namespace STD;
Int _ POW (int A, int N) {// evaluate the solution of a ^ n % 29
Int B = 1;
While (n> 1) {// This code is hard to explain. Let's take a look at the idea of binary.
If (N % 2 = 0 ){
A = A * A % 29;
N/= 2;
}
Else {
B = B * A % 29;
N --;
}
}
Return a * B % 29;
}
Int main (){
Int X, A, B, C;
While (CIN> x, x) {// The Reverse element is obtained directly.
A = _ POW (2, 2 * x + 1 );
B = _ POW (3, (x + 1 ));
C = _ POW (22, (x + 1 ));
Cout <(A-1) * (b-1) * 15 * (c-1) * 18% 29 <Endl;
}
}