1008: [HNOI2008]越獄[簡單題]
阿新 • • 發佈:2018-11-07
題意:監獄有連續編號為1…N的N個房間,每個房間關押一個犯人,有M種宗教,每個犯人可能信仰其中一種。如果
相鄰房間的犯人的宗教相同,就可能發生越獄,求有多少種狀態可能發生越獄
題解:我們考慮用總的方案減掉不能越獄的方案。也就是相鄰兩個不同的方案數。
拓展:如果題目改為恰好使用M種宗教的話那麼做法為二項式反演了,因為可以反演出至多用M種宗教的方案。
/**************************************************************
Problem: 1008
User: ReJ
Language: C++
Result: Accepted
Time:44 ms
Memory:1288 kb
****************************************************************/
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define g getchar()
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, a, b) for(int i = a; i <= b; i++)
#define per(i, a, b) for(int i = a; i >= b; i--)
#define fi first
#define se second
const int maxn = 1e5 + 10 ;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const int mod = 100003;
inline ll read() {
ll x = 0, f = 1;
char ch = g;
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = g;}
while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = g;}
//cout << f * x << endl;
return f * x;
}
ll qp(ll base, ll n) {
ll res = 1;
while(n) {
if(n & 1) res = res * base % mod;
base = base * base % mod;
n >>= 1;
}
return res;
}
int main() {
ll m, n;
// scanf("%lld %lld", &m, &n);
m = read(); n = read();
printf("%lld\n", (qp(m, n) % mod - m * qp(m - 1, n - 1) % mod + mod) % mod);
return 0;
}