D - 越狱
监狱有连续编号为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)
1 #include <iostream> 2 using namespace std; 3 #include<string.h> 4 #include<set> 5 #include<stdio.h> 6 #include<math.h> 7 #include<queue> 8 #include<map> 9 #include<algorithm> 10 #include<cstdio> 11 #include<cmath> 12 #include<cstring> 13 #include <cstdio> 14 #include <cstdlib> 15 #include<cstring> 16 long long n,m; 17 long long mo = 100003; 18 long long TM(long long a,long long b) 19 { 20 long long sum=1; // 传说中的快速幂 21 a%=mo; 22 while(b) 23 { 24 if(b%2==1) 25 sum=sum*a%mo; 26 a=a*a%mo; 27 b=b/2; 28 } 29 return sum; 30 } 31 int main() 32 { 33 while(cin>>n>>m) 34 { 35 long long s=(TM(n,m)-n%mo*TM(n-1,m-1)%mo+mo)%mo; 36 cout<<s<<endl; 37 } 38 return 0; 39 }
快速幂 考虑不会越狱的情况