BZOJ 1008: [HNOI2008]越狱 快速幂
1008: [HNOI2008]越狱
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)
Source
题解: // ans = m^n - m* (m-1)^(n-1);
//meek///#include<bits/stdc++.h> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include<iostream> #include<bitset> using namespace std ; #define mem(a) memset(a,0,sizeof(a)) #define pb push_back #define fi first #define se second #define MP make_pair typedef long long ll; const int N = 100010; const int inf = 0x3f3f3f3f; const int MOD = 100003; // ans = m^n - m* (m-1)^(n-1); ll n,m; ll quick_pow(ll x,ll p) { if(!p) return 1; ll ans = quick_pow(x,p>>1); ans = ans*ans%MOD; if(p & 1) ans = ans*x%MOD; return ans; } int main() { scanf("%lld%lld",&m,&n); printf("%lld\n",(quick_pow(m,n)+MOD-m*quick_pow(m-1,n-1)%MOD)%MOD); }