Description
为了使得大家高兴,小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
HINT
数据范围
30%的数据n<=4,m<=10,k<=10
另有20%的数据k=0
70%的数据n<=1000,m<=1000,k<=1000
100%的数据 n<=109,m<=109,k<=105,1<=y<=n,1<=x<=m
Source
傻逼题
每一位数当成(n+1)*n/2,减掉不同的限制条件就行了
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#define LL long long
#define MAXN 100100
#define P 1000000007
using namespace std;
int n,m,k;
LL ans,sum,tmp;
void in(int &x)
{
x=0;char ch=getchar();int flag=1;
while (!(ch>='0'&&ch<='9')) flag=ch=='-'?-1:flag,ch=getchar();
while (ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();x*=flag;
}
struct edge
{
int u,v;
bool operator <(const edge& a)const
{
return u==a.u?v<a.v:u<a.u;
}
}s[MAXN];
LL Pow(LL x,LL n)
{
LL ret=1;
for (LL i=n;i;i>>=1,x=x*x%P)
if (i&1) ret=ret*x%P;
return (ret+P)%P;
}
int main()
{
in(n);in(m);in(k);
for (int i=1;i<=k;i++) in(s[i].u),in(s[i].v);
sort(s+1,s+k+1);sum=(LL)n*(n+1)%P/2;tmp=sum;ans=1;
for (int i=1;i<=k;i++)
{
if (s[i].u!=s[i-1].u&&i!=1) ans=ans*tmp%P,tmp=sum,m--;
if (s[i].u!=s[i-1].u||s[i].v!=s[i-1].v) tmp-=s[i].v,tmp=tmp<0?tmp+P:tmp;
}
m--;ans=ans*tmp%P;ans=ans*Pow(sum,m)%P;
cout<<ans<<endl;
}