洛谷P3197 [HNOI2008]越狱
题目描述

题目分析
可以利用容斥的思想,答案为所有的方案减去所有合法的方案。
所有方案有\(m^n\)种。
所有合法的方案为\(m*(m-1)^{n-1}\)种。
所以答案为\(m^n-m*(m-1)^{n-1}\),利用快速幂即可
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int mod = 100003;
LL n, m;
int qmi(LL k, LL a) {
int res = 1;
while (k) {
if (k & 1) res = (LL)res * a % mod;
a = (LL)a * a % mod;
k >>= 1;
}
return res;
}
int main() {
cin >> m >> n;
cout << (qmi(n, m) - (LL)qmi(n - 1, m - 1) * m % mod + mod) % mod << endl;
return 0;
}

浙公网安备 33010602011771号