zhber
有好多做过的题没写下来,如果我还能记得就补吧

 

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

 

首先易知答案就是每一个数的所有可以取的数之和乘在一起


然后因为限制很小,就读进来排序+乱搞。然后因为m是10e,还要快速幂处理没有约束的

#include<cstdio>
#include<algorithm>
using namespace std;
#define LL long long
#define mod 1000000007
#define K 100010
struct lim{
	int x,y;
}l[K];
LL n,m,tot,ans=1,mul,tomul;
int k,cnt;
inline bool cmp(const lim &a,const lim &b)
{return a.x<b.x||a.x==b.x&&a.y<b.y;}
inline void quickpow(LL &ans,LL a,LL b)
{
	LL mult=a;
	while (b)
	{
		if (b&1)ans=(ans*mult)%mod;
		mult=(mult*mult)%mod;
		b>>=1;
	}
}
inline LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
int main()
{
	n=read();m=read();k=read();mul=(n*(n+1)/2)%mod;
	LL x,y;
	for(int i=1;i<=k;i++)
	{
		l[i].x=read();l[i].y=read();
	}
	sort(l+1,l+k+1,cmp);
	tot=m;tomul=mul-l[1].y;
	for (int i=2;i<=k;i++)
	{
		if (l[i].x==l[i-1].x)
		{
			if (l[i].y==l[i-1].y)continue;
			tomul-=l[i].y;
		}else
		{
			tot--;
			if (tomul<0)tomul=tomul%mod+mod;
			ans=(ans*tomul)%mod;
			tomul=mul-l[i].y;
		}
	}
	if (mul!=tomul)
	{
		tot--;
		if (tomul<0)tomul=tomul%mod+mod;
		ans=(ans*tomul)%mod;
	}
	quickpow(ans,mul,tot);
	printf("%lld",ans);
}

  

posted on 2014-08-09 22:18  zhber  阅读(178)  评论(0编辑  收藏  举报