[AcWing 1290] 越狱

image

全集 - 集合的补集


点击查看代码
#include<bits/stdc++.h>

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;
const LL mod = 100003;

LL n, m;

LL qmi(LL a, LL b, LL p)
{
    LL res = 1;
    while (b) {
        if (b & 1)
            res = res * a % p;
        b >>= 1;
        a = a * a % p;
    }
    return res;
}

inline LL get(LL x, LL mod)
{
    return (x % mod + mod) % mod;
}

void solve()
{
    cin >> m >> n;
    LL t = qmi(m, n, mod);
    LL c = m * qmi(m - 1, n - 1, mod);
    LL res = get(t - c, mod);
    cout << res << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    solve();

    return 0;
}

  1. 补集的思想
    ① 全集是所有的可能方案,每个人都有 \(m\) 种选择,一共有 \(m^{n}\)
    ② 集合的补集是没有越狱的方案,第一个人有 \(m\) 种选择,后面的每个人只要不与前一个人相同即可,每个人都有 \(m - 1\) 种选择,一共有 \(m \cdot (m - 1)^{n - 1}\)
    最终的答案即为全集减去补集,即 \(res = m^{n} - m \cdot (m - 1)^{n - 1}\)
posted @ 2022-08-03 23:30  wKingYu  阅读(20)  评论(0编辑  收藏  举报