1008: [HNOI2008]越狱
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 4119 Solved: 1749
[ Submit][ Status]
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
水题
从反面来求,总的方法数减去不越狱的方法数就是答案
根据乘法原理可以很简单的写出
然后用快速幂来求即可
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#define oform1 "%I64d"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#define oform1 "%lld"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define P64I1(a) printf(oform1, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 1e-9;
const double PI = (4.0*atan(1.0));
const int MOD = 100003;
LL powmod(LL a, LL p, LL C) {
if(p == 0) return 1;
LL ret = powmod(a, p>>1, C);
ret = (ret * ret) % C;
if(p&1) ret = (ret * a) % C;
return ret;
}
int main() {
LL m, n;
scanf("%lld%lld", &m, &n);
LL all = powmod(m, n, MOD);
LL no = powmod(m-1, n-1, MOD);
no = (no * m) % MOD;
LL ans = (all + MOD - no) % MOD;
printf("%lld\n", ans);
return 0;
}