1008: [HNOI2008] Jailbreak time limit:1 Sec Memory limit:162 MB
submit:5247 solved:2270
[Submit] [Status] [Discuss] 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)
This question belongs to a combinatorial mathematics problem, I thought at the beginning is DP ....
All possible religious schemes are: M^n, because there are N prisons, each prison prisoner's religion has a choice of M
The scheme for a person who cannot escape (the religious beliefs of the next two rooms) is: m* (M-1) ^ (N-1), the prisoner of the first prison can have M religious choice, the prisoner in the second prison can have a choice of M-1 (guarantee and the prisoner in the first prison is not repeated), similarly, The prisoner in the third prison is not the same as in the second prison, it is also M-1 options,,, and so on, a total of N prisons, except the first prison has M choice, the rest N-1 a prison only M-1 choice, so m* (M-1) ^ (N-1)
So the final answer: [m^n-m* (M-1) ^ (N-1)]%100003
Because the n,m are relatively large, so with the help of the Fast power (explanation: www.cnblogs.com/CXCXCXC/p/4641812.html)
In the middle there are a lot of details need mod, everywhere mod,mod think about right, and ((m^n)%mod-(m* (M-1) ^ (N-1)%mod)%mod may be negative, can be sentenced, if Ans<0,ans+=mod can ((m^n)%mod+ Mod-(m* (M-1) ^ (N-1))%mod)%mod in the middle add a mod
1#include <bits/stdc++.h>2 using namespacestd;3typedefLong LongLL;4 Const intMod=100003;5 LL m,n;6 LL ans;7 8 ll Poww (ll A,ll b) {9LLBase=A;TenLL ans1=1; One while(b!=0){ A if(b&1!=0){ -Ans1=ans1*Base; -ans1=ans1%MoD; the } - Base=Base*Base; - Base=Base%MoD; -B=b>>1; + } - returnans1%MoD; + } A intMain () { atCin>>m>>N; -Ans= (Poww (m,n) +mod-(M*poww (M-1, N-1))%mod)%MoD; -cout<<ans; - return 0; -}
[HNOI2008] Jailbreak