Description
The prison has consecutively numbered 1 ... n rooms of N, one prisoner in each room, a religion of M, and every prisoner may believe in one. If the inmates of the adjoining room are of the same religion, a jailbreak may occur, begging for the number of possible escapes
Input
Enter a two integer m,n.1<=m<=10^8,1<=n<=10^12
Output
Number of possible jailbreak states, modulo 100003 take-up
Sample Input2 3Sample Output6HINT
6 states of (000) (001) (011) (100) (110) (111)
SourceSolution
Answer = Total possible number-possible number of not jailbroken
= M^n-m * (m-1) ^ (n-1)
1#include <cstdio>2 #defineMOD 1000033 using namespacestd;4typedefLong Longll;5 ll M, N;6 7 intKSM (ll A, ll b)8 {9ll C =1, d = a%MOD;Ten while(b) One { A if(B &1) -c = c * d%MOD; -b >>=1; theD = d * d%MOD; - } - returnC; - } + - intMain () + { Ascanf"%lld%lld", &m, &n); atll ans =KSM (M, n); -Ans-= m * KSM (M-1N1) %MOD; - while(Ans <0) ans + =MOD; -printf"%lld", ans); - return 0; -}
View Code
[BZOJ1008] [HNOI2008] Jailbreak (math)