NPY and girls

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 790    Accepted Submission(s): 280

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
 
题意:有n个班级,询问在[l,r]区间内班级女生的排列方式有多少种,比如说 1 1 2,那么就有三种 1 1 2 ,2 1 1,1 2 1。
题解:首次接触莫队算法。
莫队算法:可以解决一类静态,离线区间查询问题。http://blog.csdn.net/bossup/article/details/39236275
发明这个的人好强啊,没看懂分块,不过用起来的话还是会用模板的.[l,r]里面的数字假设有m个,每个班级有c[i]个人,那么排列组合的总数为 m!/ ( c[1]! * .......c[n]! ) 关键是怎么用莫对算法求解这个式子。当区间从 [l,r-1] - >[l,r]时,那么假设之前的区间[l,r-1]里面的排列数为 N1,那么[l,r]的结果应当为 N1*(r+l-1)/c[a[r]++] ,反之,当现在的区间从
[l,r+1]->[l,r]时,假设 [l,r+1]里面的排列数为 N2,那么当前[l,r]里面的排列数应该变成了 N2*c[a[r+1]++]/(r-l+1)个人.还有左边区间两种情况可以仿造这两种情况得出来。
由于有取模操作,所以除法要用上逆元.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL mod = ;
const int N = ;
int n,m;
LL a[N];
LL inv[N];
LL Hash[N];
LL res[N];
struct Node{
int l,r,sqr,id;
}node[N];
int cmp(Node a,Node b){
if(a.sqr==b.sqr) return a.r<b.r;
return a.l<b.l;
}
LL pow_mod(LL a,LL n){
LL ans = ;
while(n){
if(n&) ans = ans*a%mod;
a = a*a%mod;
n>>=;
}
return ans;
}
LL getinv(int x){
return pow_mod(x,mod-);
}
int main()
{
int tcase;
scanf("%d",&tcase);
for(int i=;i<=N;i++){
inv[i] = getinv(i);
}
while(tcase--)
{
scanf("%d%d",&n,&m);
int k = (int)sqrt(n);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
for(int i=;i<=m;i++){
scanf("%d%d",&node[i].l,&node[i].r);
node[i].id = i;
node[i].sqr = node[i].l/k;
}
sort(node+,node+m+,cmp);
memset(Hash,,sizeof(Hash));
int l=,r = ;
Hash[a[]]++;
LL ans = ;
for(int i=;i<=m;i++){
while(r<node[i].r){
r++;
Hash[a[r]]++;
ans = ans*(r-l+)%mod*inv[Hash[a[r]]]%mod;
}
while(l>node[i].l){
l--;
Hash[a[l]]++;
ans = ans*(r-l+)%mod*inv[Hash[a[l]]]%mod;
}
while(r>node[i].r){
ans = ans*Hash[a[r]]%mod*inv[r-l+]%mod;
Hash[a[r]]--;
r--;
}
while(l<node[i].l){
ans = ans*Hash[a[l]]%mod*inv[r-l+]%mod;
Hash[a[l]]--;
l++;
}
res[node[i].id] = ans;
}
for(int i=;i<=m;i++){
printf("%lld\n",res[i]);
}
}
return ;
}

hdu 5145(莫队算法+逆元)的更多相关文章

  1. HDU 4358 莫队算法+dfs序+离散化

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

  2. HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = C ...

  3. Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元

    题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...

  4. HDU 4638 莫队算法

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

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

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

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

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

  7. HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...

  8. HDU 3333 Turing Tree 莫队算法

    题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...

  9. HDU 5381 The sum of gcd (技巧,莫队算法)

    题意:有一个含n个元素的序列,接下来有q个询问区间,对每个询问区间输出其 f(L,R) 值. 思路: 天真单纯地以为是道超级水题,不管多少个询问,计算量顶多就是O(n2) ,就是暴力穷举每个区间,再直 ...

随机推荐

  1. 4.Mongodb数据查询2

    1.limit &skip (1)Limit 方法limit():用于读取指定数量的文档 语法: db.集合名称.find().limit(NUMBER) 参数NUMBER表示要获取文档的条数 ...

  2. luogu4196 [CQOI2006]凸多边形 半平面交

    据说pkusc出了好几年半平面交了,我也来水一发 ref #include <algorithm> #include <iostream> #include <cstdi ...

  3. 什么情况使用 weak 关键字,相比 assign 有什么不同?

    什么情况使用 weak 关键字? 在 ARC 中,在有可能出现循环引用的时候,往往要通过让其中一端使用 weak 来解决,比如: delegate 代理属性 自身已经对它进行一次强引用,没有必要再强引 ...

  4. 《Cracking the Coding Interview》——第17章:普通题——题目8

    2014-04-28 23:35 题目:最大子数组和问题. 解法:O(n)解法. 代码: // 17.8 Find the consecutive subarray with maximum sum ...

  5. 《Cracking the Coding Interview》——第8章:面向对象设计——题目7

    2014-04-23 23:38 题目:你要如何设计一个聊天服务器,有什么技术难点? 解法:这是基于工作经验的面试题吗?否则,一个new grad碰上这种题目能打点草稿也就算不错了. 代码: // 8 ...

  6. flask 基础ssti注入

    源代码地址 (请用python2.7运行,python3有点出入) 注入点: 不是返回的静态模板而是反回模板字符串变得让客户端可以控制. XSS 这里直接 http://39.105.116.195: ...

  7. 更改maven本地仓库地址

    1.进入maven安装conf文件中,编辑settings.xml文件,新增图中的圈出的内容(我想要存放的地址是D:\HMY\m2\repository) 2.复制settings.xml文件至D:\ ...

  8. python-压缩解压

    压缩解压包 #导入模块 import zipfile #新建压缩包并将db与ooo.xml压缩到文件中 z = zipfile.ZipFile('laxi.zip','w') z.write('db' ...

  9. Scrapy爬取到的中文数据乱码问题处理

    Scrapy爬取到中文数据默认是 Unicode编码的,于是显示是这样的: "country": ["\u56fd\u4ea7\u6c7d\u8f66\u6807\u5f ...

  10. 团队项目-第六次Scrum 会议

    时间:11.1 时长:30分钟 地点:F楼2层沙发休息处 工作情况 团队成员 已完成任务 待完成任务 解小锐 完成员工信息的简单初始化 学习cocos creator样例 陈鑫 完成CurrentPr ...