bzoj 2751 [HAOI2012]容易题(easy) 数学

题面

题目传送门

解法

考虑当所有数都没有限制的时候,答案为\(\sum\prod_{i=1}^na_i\)

考虑将式子拆开并且运用乘法分配律,答案显然为\((\frac{n(n+1)}{2})^m\)

有限制类似

注意去重

代码

#include <bits/stdc++.h>
#define Mod 1000000007
#define int long long
#define N 100010
using namespace std;
template <typename node> void read(node &x) {
    x = 0; int f = 1; char c = getchar();
    while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
    while (isdigit(c)) x = x * 10 + c - '0', c = getchar(); x *= f;
}
struct Node {
    int x, y;
    bool operator < (const Node &a) const {
        if (x == a.x) return y < a.y;
        return x < a.x;
    }
} a[N];
int Pow(int x, int y) {
    int ret = 1;
    while (y) {
        if (y & 1) ret = ret * x % Mod;
        y >>= 1, x = x * x % Mod;
    }
    return ret;
}
main() {
    int n, m, k; read(n), read(m), read(k);
    for (int i = 1; i <= k; i++)
        read(a[i].x), read(a[i].y);
    sort(a + 1, a + k + 1);
    int tot = 0, ans = 1, s = 1, sum = n * (n + 1) / 2 % Mod;
    map <int, int> h;
    for (int i = 1; i <= k; i++) {
        if (a[i].x != a[i - 1].x)
            tot++, ans = ans * s % Mod, s = sum, h.clear();
        if (!h.count(a[i].y)) s = (s - a[i].y + Mod) % Mod, h[a[i].y] = 1;
    }
    ans = ans * s % Mod * Pow(sum, m - tot) % Mod;
    cout << ans << "\n";
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/copperoxide/p/9478694.html