The question wa has been a lot of back... sad reminder... you cannot directly reverse the yuan for computation. You still need to use the tips in number theory...
Stick to the question of shenniu ..
// This subject requires S
// Conclusion: S = (251 ^ (n + 1)-1) * (2 ^ (3n + 1)-1)/250
// Two equal-ratio series and multiplication
//
// Reasoning:
// 2008 = 2 ^ 3*251
// So 2008 ^ N has 3 N 2 and N 251
// All the factors that only consist of 2 are
// 2 ^ 0 2 ^ 1 2 ^ 2... 2 ^ (3N)
// Set the set C = {2 ^ 0, 2 ^ 1, 2 ^ 2..., 2 ^ (3N )};
// SUM (C) = 2 ^ (3n + 1)-1
// The factors produced in combination with 251 are:
// 251 ^ 0 * C
// 251 ^ 1 * C
//...
// 251 ^ N * C
// The sum of all factors is:
// S = (251 ^ (n + 1)-1)/250*(2 ^ (3n + 1)-1)
// Calculate S % K:
// S is large and cannot be stored in common data types. You need to calculate S % K directly.
// Because S has a denominator of 250, set S to X/250
// S % K = (X/250) % K = (X % (250 * K)/250
// The form of first finding the remainder and then Division
Once you know the techniques in number theory, you cannot use your back-to-source to find out, because not meeting the requirements of gcd (250, k) or gcd (2, k) is not necessarily of mutual quality, the code is no longer a problem,
# Include <stdio. h>
Int mult (int a1, int n, int k ){
If (n = 0) return 1;
_ Int64 B = 1, a = a1;
While (n> 1 ){
If (n % 2 = 0 ){
A = a * a % k;
N/= 2;
}
Else {
B = B * a % k;
N --;
}
}
Return a * B % k;
}
Int main (){
Int n, k;
While (scanf ("% d", & n, & k), n & k ){
_ Int64 a = mult (2, 3 * n + 1,250 * k );
_ Int64 B = mult (251, n + 1,250 * k );
A --, B --;
A = (a * B) % (250 * k );
A/= 250;
Printf ("% d \ n", mult (2008, a, k ));
}
}