pid=5145">【HDU 5145】 NPY and girls(组合+莫队)

NPY and girls

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 593    Accepted Submission(s): 179

Problem Description
NPY's girlfriend blew him out!His honey doesn't love him any more!However, he has so many girlfriend candidates.Because there are too many girls and for the convenience of management, NPY numbered the girls from 1 to n.These girls
are in different classes(some girls may be in the same class).And the i-th girl is in class ai.NPY wants to visit his girls frequently.Each time he visits some girls numbered consecutively from L to R in some order. He can only visit one girl every time he
goes into a classroom,otherwise the girls may fight with each other(-_-!).And he can visit the class in any order.

Here comes the problem,(NPY doesn't want to learn how to use excavator),he wonders how many different ways there can be in which he can visit his girls.The different ways are different means he visits these classrooms in different order.
 
Input
The first line contains the number of test cases
T(1≤T≤10).

For each test case,there are two integers n,m(0<n,m≤30000)
in the first line.N is the number of girls,and M is the number of times that NPY want to visit his girls.

The following single line contains N integers, a1,a2,a3,…,an,
which indicates the class number of each girl. (0<ai≤30000)

The following m lines,each line contains two integers
l,r(1≤l≤r≤n),which
indicates the interval NPY wants to visit.
 
Output
For each visit,print how many ways can NPY visit his girls.Because the ans may be too large,print the ans mod 1000000007.
 
Sample Input
2
4 2
1 2 1 3
1 3
1 4
1 1
1
1 1
 
Sample Output
3
12
1
 
Source
 
Recommend
heyang
 

题目大意:T组输入

每组两个数 n m分别表示人数n和询问次数m

之后n个数 表示n个女孩所在教室

对于m次询问 每次一个[L,R](1 <= L <= R <= n) 问为了訪问到每一个女孩 訪问教室的方案有几种(会出现几个女孩在一个教室的情况 但每次訪问教室仅仅能找一个女孩 同一个编号的教室是同样的)

对于单组询问[L,R] 如果有n中班级 每一个班级有c个女生 总共m个女生 那么答案就是C(m,c1)*C(m-c1,c2)*C(m-c1-c2,c3)*....*C(cn,cn)

但假设每次都暴力的进行统计然后求组合数 果果的会超时

这样就要用莫队 把区间n进行分块 分成n/sqrt(n)份 进行分块排序

然后按分好的顺序进行区间的扩大或缩小 这样能够发现对组合数的求解也能够在区间转换的过程中进行

由于C(m+1,n+1) = C(m,n)*(m+1/n+1)

相同的 C(m,n) = C(m+1,n+1)*(n+1/m+1) (对于缩小的过程而言)

这样 因为n和m的范围都在30000之内 并且要求最后对一个大素数取余 能够用费马小定理高速求出范围内全部数的逆元

然后就像上面说的对区间进行转换 然后离线处理存储答案 最后输出就可以 记得答案要存在排序前的位置中。。。由于这个WA找了好久错误=.=

代码例如以下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const LL mod = 1e9+7;
const double eps = 1e-8; LL pow_m(LL a,LL b)
{
LL ans = 1;
while(b)
{
if(b&1) ans = (ans*a)%mod;
b >>= 1;
a = (a*a)%mod;
}
return ans;
} LL Inv(LL a)
{
return pow_m(a,mod-2);
} int per;
struct Range
{
int l,r,id;
bool operator < (const struct Range a)const
{
return l/per == a.l/per? r < a.r: l/per < a.l/per;
}
}; Range rg[30030];
LL ans[30030];
LL inv[30030];
int cla[30030];
int cnt[30030]; int main()
{
//fread();
//fwrite(); for(int i = 1; i <= 30000; ++i)
inv[i] = Inv(i); int t,n,q; scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
per = sqrt(n*1.0); for(int i = 1; i <= n; ++i)
scanf("%d",&cla[i]); for(int i = 0; i < q; ++i)
{
scanf("%d%d",&rg[i].l,&rg[i].r);
rg[i].id = i;
} sort(rg,rg+q); int l = 1, r = 0;
LL tmp = 1;
memset(cnt,0,sizeof(cnt));
for(int i = 0; i < q; ++i)
{
while(r < rg[i].r)
{
++r;
cnt[cla[r]]++;
tmp = tmp*(r-l+1)%mod*inv[cnt[cla[r]]]%mod;
}
while(l > rg[i].l)
{
--l;
cnt[cla[l]]++;
tmp = tmp*(r-l+1)%mod*inv[cnt[cla[l]]]%mod;
} while(r > rg[i].r)
{
tmp = tmp*cnt[cla[r]]%mod*inv[r-l+1]%mod;
cnt[cla[r]]--;
--r;
} while(l < rg[i].l)
{
tmp = tmp*cnt[cla[l]]%mod*inv[r-l+1]%mod;
cnt[cla[l]]--;
++l;
}
ans[rg[i].id] = tmp;
}
for(int i = 0; i < q; ++i)
printf("%I64d\n",ans[i]); } return 0;
}

【HDU 5145】 NPY and girls(组合+莫队)的更多相关文章

  1. HDU 5145 NPY and girls(莫队算法+乘法逆元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...

  2. HDU 5145 NPY and girls (莫队分块离线)

    题目地址:HDU 5145 莫队真的好奇妙.. 这种复杂度竟然仅仅有n*sqrt(n)... 裸的莫队分块,先离线.然后按左端点分块,按块数作为第一关键字排序.然后按r值作为第二关键字进行排序. 都是 ...

  3. HDU 5145 NPY and girls 莫队+逆元

    NPY and girls Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more! ...

  4. HDU 5145 - NPY and girls

    题意: cases T(1≤T≤10) (0<n,m≤30000) (0<ai≤30000)    n个数ai 表示n个女孩所在教室 m次询问 [L,R](1 <= L <= ...

  5. HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)

    传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...

  6. hdu 5381 The sum of gcd 莫队+预处理

    The sum of gcd Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) P ...

  7. HDU 4676 Sum Of Gcd 【莫队 + 欧拉】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=4676 Sum Of Gcd Time Limit: 10000/5000 MS (Java/Others ...

  8. hdu 4358 Boring counting dfs序+莫队+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  9. HDU 4358 Boring counting dfs序+莫队算法

    题意:N个节点的有根树,每个节点有一个weight.有Q个查询,问在以u为根的子树中,有恰好出现了K次的weight有多少种. 这是第一次写莫队算法,之前也只是偶有耳闻. 看了别人的代码打的,还是贴上 ...

随机推荐

  1. 紫书 习题7-14 UVa 307(暴搜+剪枝)

    这道题一开始我想的是在排序之后只在头和尾往中间靠近来找木块, 然后就WA, 事实证明这种方法是错误的. 然后参考了别人的博客.发现别人是直接暴搜, 但是加了很多剪枝, 所以不会超时. 我也想过这个做法 ...

  2. where和having

    where可以不能使用别名作为过滤条件,而having可以使用别名作为过滤条件. 在ORACLE中,select 语句的执行顺序是: 1. from语句 2. where语句(结合条件) 3. sta ...

  3. JS中常用开发知识点

     JS中常用开发知识点 1.获取指定范围内的随机数 2.随机获取数组中的元素 3.生成从0到指定值的数字数组 等同于: 4.打乱数字数组的顺序 5.对象转换为数组 //注意对象必须是以下格式的才可以通 ...

  4. php 魔术方法和魔术常量

    魔术方法:PHP把类中所有以__(两个下划线)开头的方法当成魔术方法,一般建议用户不要将自定义的方法前面加上__作为前缀.魔术方法: 1. __construct() 类的默认构造方法,如果__con ...

  5. ActiveMQ_Windows和Linux版本的安装部署

    1, 保证电脑上安装了jdk6以上版本的java,并配置了好环境变量 : 2, 官方下载地址:http://activemq.apache.org/download-archives.html ,这里 ...

  6. ASP.NET-属性与过滤器

    目的:在调用操作之前或者之后执行特定的逻辑代码 系统定义: 1.日志记录 2.防图像盗链  3.爬虫 4.本地化,用于设定区域设置 5.动态操作,用于将操作注入到控制器当中 用来过滤HTTP请求 高级 ...

  7. ASP.NET-HTTP响应标头

    Reponse Headers 理论上所有的响应头信息都应该是回应请求头的.但是服务端为了效率,安全,还有其他方面的考虑,会添加相对应的响应头信息,从上图可以看到: Cache-Control:mus ...

  8. JavaCodeTra 36选7 彩票抽奖

    想写个小代码试试自己的运气.然并卵.并不能猜中 import java.util.Random; import java.util.Scanner; /** * */ /** * @author Ha ...

  9. hdoj 1013Digital Roots

     /*Digital Roots Problem Description The digital root of a positive integer is found by summing th ...

  10. iOS定义静态变量、静态常量、全局变量

    静态变量 当我们希望一个变量的作用域不仅仅是作用域某个类的某个对象,而是作用域整个类的时候,这时候就可以使用静态变量. staticstatic修饰的变量,是一个私有的全局变量.C或者Java中sta ...