1008: [HNOI2008] Jailbreak time limit:1 Sec Memory limit:162 MB
submit:5535 solved:2370
[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 is a simple combinatorial math problem. In all cases, there are MN species schemes. There are m* (m-1) (n-1) species that are unlikely to occur. The former is good to understand, the latter I want to explain why. We abstract the problem into a model, lattice staining. There are different kinds of schemes for the color of two adjacent lattices. First, the first lattice is capable of dyeing m colors. The second lattice in order not to conflict with the previous lattice can be dyed m-1 color, and so on. It is concluded that except for the first lattice, the rest of the lattice can only dye m-1 color. Finally, the answer is mn-m* (m-1) (n-1), and finally take a model. We can use a fast power to calculate n times. Time complexity O (log2n)
#include <cstdio>#include<cstring>#include<iostream>using namespacestd;Const intMOD =100003; Long LongN;Long Longm;Long LongpwLong LongALong Longb) { if(A = =0|| A = =1)returnA; BOOLp[ -]; intLen =0; Long Longans; Do{P[len+ +] = (b &1); b>>=1; } while(b); Ans=1; for(inti = len-1; I >=0; --i) {ans= ans * ans%MOD; if(P[i]) ans = (ans * (a% MOD))%MOD; } returnans;} intMain () {CIN>> m >>N; cout<< ((PW (M, N)-(m% MOD) * PW (M-1N1)% mod + MoD) <<Endl; return 0;}
"Bzoj 1008" [HNOI2008] Jailbreak