1008: [HNOI2008]越狱
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 10626 Solved: 4613
[ Submit][ Status][ Discuss]
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
最简单的组合数了吧。。
不发生越狱的组合 Cm1 * C(m - 1)1 * ....* C(m - 1) 1。及 M * (M - 1) ^(n - 1)
发生越狱的组合 Cm1 * Cm1 *....* Cm1 M * M ^ (n - 1)
相减就完了
注意longlong
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 51550;
const double esp = 1e-9;
const int mod = 100003;
long long pow(long long n, long long m) {
long long res = 1, base = n;
while (m) {
if (m & 1)
res *= base;
res %= mod;
base *= base;
base %= mod;
m >>= 1;
}
return res;
}
int main() {
//freopen("in.txt", "r", stdin);
long long n, m;
cin >> m >> n;
long long not_can = ::pow(m - 1, n - 1), can = ::pow(m, n - 1);
long long ans = (can - not_can + mod) % mod;
cout << (ans * m) % mod << endl;
}