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 titled A combinatorial Mathematics topic, I am the mathematics konjac Konjac, looked the problem after only then understood. In fact, the key is reverse thinking.
N Rooms m religions, the total number of states that is m^n, if not jailbreak, that is, adjacent rooms of two people religion is different, so the first person has m choice, then each person has m-1 choice, so not jailbreak the total number of States I m* (m-1) ^ (n-1); so the number of jailbreak states is m^n-m* ( m-1) ^ (n-1).
Solve this problem, the quick power to solve the good.
#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>#defineMoD 100003#definell Long Longusing namespacestd;ll m,n;ll Qpow (ll A,ll b) {ll C=1, d=a%MoD; while(b>0) { if(b&1) C= (c%mod*d%mod)%MoD; b>>=1; D= (d%mod*d%mod)%MoD; } returnC;}intMain () {scanf ("%lld%lld",&m,&N); Long Longans=Qpow (m,n); Ans=ans-m*qpow (M-1, N-1)%MoD; if(ans<0) ans+=MoD; printf ("%lld", ans); return 0;}
[HNOI2008] [BZOJ1008] Jailbreak