2N——数论+快速幂

题目

为了使得大家高兴,小Q特意出个自认为的简单题(easy)来满足大家,这道简单题是描述如下: 有一个数列A已知对于所有的$A[ i ]$都是$1~n$的自然数,并且知道对于一些$A[ i]$不能取哪些值,我们定义一个数列的积为该数列所有元素的乘积,要求你求出所有可能的数列的积的和 $mod 1000000007$的值,是不是很简单呢?呵呵!

Input

第一行三个整数$n,m,k$分别表示数列元素的取值范围,数列元素个数,以及已知的限制条数。 接下来$k$行,每行两个正整数$x,y$表示$A[ x]$的值不能是$y$。

Output

一行一个整数表示所有可能的数列的积的和对$1000000007$取模后的结果。如果一个合法的数列都没有,答案输出$0$。

Sample Input

3 4 5
1 1
1 1
2 2
2 3
4 3

Sample Output

90

样例解释

$A[ 1]$不能取$1$ $A[ 2]$不能去$2、3$ $A[ 4]$不能取$3$ 所以可能的数列有以下$12$种 |数列| 积| |:-:|:-:| |2 1 1 1| 2| |2 1 1 2| 4| |2 1 2 1| 4| |2 1 2 2| 8| |2 1 3 1| 6| |2 1 3 2| 12| |3 1 1 1| 3| |3 1 1 2| 6| |3 1 2 1| 6| |3 1 2 2| 12| |3 1 3 1| 9| |3 1 3 2| 18|

数据范围

  • $30%$的数据$n<=4,m<=10,k<=10$

  • 另有$20%$的数据$k=0$

  • $70%$的数据$n<=1000,m<=1000,k<=1000$

  • $100%$的数据 \(n<=10^9,m<=10^9,k<=10^5,1<=y<=n,1<=x<=m\)

题解

解题思路

考虑如果没有限制条件,根据加法原理和乘法原理可得 \((1+2+3+……+n)^m=n*(n+1)^m\) 单独计算加上限制后这位数的情况

代码

#include <cstdio>
#include <map>
#define int long long
using namespace std;
const int N = 1e5+5, M = 1e9+7;
int qpow(int a, int x) {
    a %= M;
    int ans = 1;
    while (x) {
        if (x & 1) ans *= a % M, ans %= M;
        x >>= 1;
        a *= a % M, a %= M;
    }
    return ans % M;
}
map<pair<int , int>, int > v;
map<int, int> s;
int n, m, k, a[N], tot, ans = 1, sum;
signed main() {
    scanf("%lld%lld%lld", &n, &m, &k);
    sum = n * (n + 1) / 2;
    for(int i = 1; i <= k; i++) {
        int x, y;
        scanf("%lld%lld", &x, &y);
        if (!s[x]) a[++tot] = x;
        if (v[make_pair(x, y)]) continue;
        v[make_pair(x, y)] = 1;
        s[x] += y;
    }
    for(int i = 1; i <= tot; i++)
        ans *= (sum - s[a[i]]) % M, ans %= M;
    ans = ans * qpow(sum, m - tot) % M;
    printf("%lld", ans);
    return 0 ;
}

猜你喜欢

转载自www.cnblogs.com/Z8875/p/12887811.html