快速幂裸题
直接算逃跑的方案数不好算,我们可以用总方案数-合法方案数
总方案数是什么呢 ?一共有n个房间,m个信仰,那么每个房间都可能是任意一种,所以是m^n种方案数
合法的方案数呢?任意两个相同信仰不能挨着,那么第一个房间有m种选法,第一个房间是除了第一个房间外所有的都能选,所以是m-1种,第三个房间不能选第二个的,也是m-1种,所以合法方案数是(m-1)^(n-1)*m
相减就好了
代码
//By AcerMo
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define lli long long int
using namespace std;
const int mod=100003;
lli n,m;
lli fpow(lli a,lli b)
{
lli ans=1;
for (;b;a=(a*a)%mod,b>>=1)
if (b&1) ans=(ans*a)%mod;
return ans;
}
signed main()
{
cin>>m>>n;
lli ans=fpow(m,n);
lli que=(fpow(m-1,n-1)*m)%mod;
cout<<(ans-que+mod)%mod;
return 0;
}