bzoj2751【haoi2012】容易题(easy)
2015-11-08 22:09
344 查看
2751: [HAOI2012]容易题(easy)
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1401 Solved: 600
[Submit][Status][Discuss]
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 51 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
m范围很大,k范围很小,所以至少有(m-k)个位置是n个数都可以取,对于这些位置用快速幂计算,其余位置暴力计算。#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #define F(i,j,n) for(int i=j;i<=n;i++) #define D(i,j,n) for(int i=j;i>=n;i++) #define LL long long #define pa pair<int,int> #define mod 1000000007 #define MAXK 100005 using namespace std; int n,m,k,tot; LL sum,tmp,ans=1; struct data { int pos,val; }a[MAXK]; int read() { int ret=0,flag=1;char ch=getchar(); while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();} while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();} return ret*flag; } bool cmp(data d1,data d2) { if (d1.pos==d2.pos) return d1.val<d2.val; else return d1.pos<d2.pos; } LL pow(LL x,int y) { LL ret=1; for(;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod; return ret; } int main() { // freopen("input.in","r",stdin); n=read();tot=m=read();k=read(); F(i,1,k) a[i].pos=read(),a[i].val=read(); sort(a+1,a+k+1,cmp); tmp=sum=(LL)n*(n+1)/2%mod; F(i,1,k+1) { if (i!=1&&a[i].pos!=a[i-1].pos) { ans=ans*tmp%mod; tmp=sum; tot--; } if (a[i].pos==a[i-1].pos&&a[i].val==a[i-1].val) continue; tmp=(tmp+mod-a[i].val)%mod; } ans=ans*pow(sum,tot)%mod; printf("%lld\n",ans); return 0; }
相关文章推荐
- OC中的类簇
- [LeetCode][JavaScript]Unique Binary Search Trees II
- SurfaceFlinger服务概述和学习计划
- LintCode-比较字符串
- Windows消息之Spk++工具
- Week1-1Human language and animal communication systems
- djvulibre使用简单说明
- nginx配置ajp
- 典型用户
- 垂直居中
- 2015.11.8 ---2013.南京
- ruby调试/练习时的小技巧
- POJ1548-Robots Dilworth 定理
- HDU 1160 FatMouse's Speed(最长递减子序列变形)
- ‘文案菜鸟的蜕变之旅’
- 杀死进程、退出应用程序时提示、自定义apk安装、反编译
- 《leetCode》:permutations II
- 大水题--CodeForces - 591A Wizards' Duel
- OC工厂模式
- android学习笔记WebView的基本使用