Description
监狱有连续编号为1...N的N个房间,每个房间关押一个犯人,有M种宗教,每个犯人可能信仰其中一种。如果
相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱
Input
输入两个整数M,N.1<=M<=10^8,1<=N<=10^12
Output
可能越狱的状态数,模100003取余
Sample Input
2 3
Sample Output
6
Hint
6种状态为(000)(001)(011)(100)(110)(111)
Solution
拿到这道题,想了半天,不知道怎么找可能越狱的状态数。
其实只要换个思路我们只要找到不会越狱的状态数就行了。
第一个人有m种可能,后面的n-1个人每个人要和前面不同,有m-1种可能。
然后用快速幂搞一搞就好了。
Code
#include <cstdio>
#include <iostream>
using namespace std;
typedef long long LL;
const int mod = 100003;
LL n, m;
int mul(LL x, LL k) {
int res = 1;
for(; k; k>>=1) {if(k&1)res = res * x % mod; x = x * x % mod;}
return res;
}
int main() {
scanf("%lld%lld", &m, &n);
printf("%d\n", (mul(m, n - 1) - mul(m-1, n-1) + mod) * m % mod);
return 0;
}