Description
监狱有连续编号为
1...N
1...
N
的
N
N
个房间,每个房间关押一个犯人,有种宗教,每个犯人可能信仰其中一种。如果
相邻房间的犯人的宗教相同,就可能发生越狱,求有多少种状态可能发生越狱
Input
输入两个整数 M,N.1≤M≤108,1≤N≤1012 M , N .1 ≤ M ≤ 10 8 , 1 ≤ N ≤ 10 12
Output
可能越狱的状态数,模 100003 100003 取余
Sample Input
2 3
Sample Output
6
Solution
总情况 mn m n 种,不合法情况即为相邻房间信仰均不同,第一个房间 m m 种可能,之后每个房间的犯人由于都不能和前一个房间犯人的信仰相同,故只有种可能,故不合法方案数即为 m⋅(m−1)n−1 m ⋅ ( m − 1 ) n − 1 ,两者做差即为答案
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
const ll mod=100003;
ll Pow(ll a,ll b)
{
ll ans=1;
while(b)
{
if(b&1)ans=ans*a%mod;
a=a*a%mod;
b>>=1;
}
return ans;
}
int main()
{
ll m,n;
scanf("%lld%lld",&m,&n);
ll ans=(Pow(m,n)-m*Pow(m-1,n-1)%mod+mod)%mod;
printf("%lld\n",ans);
return 0;
}