- +mod,防止相减
+ `fastPow(m % mod, n) - (m % mod) * fastPow((m - 1) % mod, (n - 1)) % mod + mod) % mod出现负数
- 对于数据及时取模,防止溢出
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
//const int INF = 0x3f3f3f3f;
//const int MAXN = ;
const ll mod = 100003;
// int n, m;
ll fastPow(ll a, ll b) {
a %= mod;
ll ret = 1;
while (b) {
if (b % 2 == 1) ret = (ret * a % mod) % mod;
a = ((a % mod) * (a % mod)) % mod;
b /= 2;
}
return ret;
}
int main() {
#ifdef LOCAL
freopen("zz_in.txt", "r", stdin);
freopen("zz_op.txt", "w", stdout);
#endif
ll n, m;
cin >> m >> n;
cout << (fastPow(m % mod, n) - (m % mod) * fastPow((m - 1) % mod, (n - 1)) % mod + mod) % mod;
//+ mod,防止相减出现负数
#ifdef LOCAL
printf("Time used = %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
return 0;
}
快速幂的另一种写法
int fastPow(long long a, int b, int m) {
if (b == 1) return a;
if (b % 2 == 0) {
long long t = fastPow(a, b / 2, m);
//易出错,漏掉fastPow而只写了括号
return t * t % m;
} else {
long long t = fastPow(a, b / 2, m);
t = t * t % m;
t = t * a % m;
return t;
}
}