NPY and girls

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
 
题意: 
 
  给你n个数,m次询问,每次询问l,r之间有多少种排列
 
题解:  
 
  典型的莫队
  只不过这个数有点大
  需要预处理n这么大 的 逆元
 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
const int N = 5e4+, M = 4e4+, mod = 1e9+, inf = 0x3f3f3f3f; int belong[N], a[N], m, n, T;
struct ss{int l,r,id;}Q[N];
bool operator < (ss s1,ss s2) {
if(belong[s1.l] == belong[s2.l]) return s1.r < s2.r;
else return belong[s1.l] < belong[s2.l];
}
ll quick_pow(ll x,ll p) {
if(!p) return ;
ll ans = quick_pow(x,p>>);
ans = ans*ans%mod;
if(p & ) ans = ans*x%mod;
return ans;
} ll inv(ll x)
{
ll mo = mod;
return quick_pow(x,mo-);
} ll an[N], Inv[N];
ll mp[N];
int main()
{
for(ll i = ; i <= ; ++i) Inv[i] = inv(i);
scanf("%d",&T);
while(T--) {
scanf("%d%d",&n,&m);
for(int i = ; i <= n; ++i) scanf("%d",&a[i]);
int t = sqrt(n);
for(int i = ; i <= n; ++i) belong[i] = (i-) / t + ;
for(int i = ; i <= m; ++i) {
scanf("%d%d",&Q[i].l,&Q[i].r);Q[i].id = i;
}
memset(mp,,sizeof(mp));
sort(Q + , Q + m + );
ll l = , r = , len = ;
ll ans = ;
for(int i = ; i <= m; ++i) {
for(;r<Q[i].r;r++) {
++len;
++mp[a[r+]];
ans = ans * len % mod;
ans = ans * Inv[mp[a[r+]]] % mod;
}
for(;l>Q[i].l;l--) {
++len;
++mp[a[l-]];
ans = ans * len % mod;
ans = ans * Inv[mp[a[l-]]] % mod;
}
for(;r>Q[i].r;r--) {
--len;
--mp[a[r]];
ans = ans * Inv[len+] % mod;
ans = ans * (mp[a[r]] + 1ll) % mod;
}
for(;l<Q[i].l;l++) {
--len;
--mp[a[l]];
ans = ans * Inv[len+] % mod;
ans = ans * (mp[a[l]] + 1ll) % mod;
}
an[Q[i].id] = ans;
}
for(int i = ; i <= m; ++i) printf("%I64d\n",an[i] % mod);
}
}

HDU 5145 NPY and girls 莫队+逆元的更多相关文章

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

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

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

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

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

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

  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. NPY and girls-HDU5145莫队算法

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

  6. hdu_5145_NPY and girls(莫队算法+组合)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5145 题意:给你n,m,共有n个女孩,标号为1—n,n个数xi表示第ith个女孩在第xi个教室,然后下 ...

  7. HDU_1175_莫队+逆元

    http://acm.hdu.edu.cn/showproblem.php?pid=5145 初探莫队,就是离线排序后的暴力,但是效率大大提高,中间要除法取模,所以用到了逆元. #include< ...

  8. HDU 4358 Boring counting(莫队+DFS序+离散化)

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

  9. hdu 4676 Sum Of Gcd 莫队+phi反演

    Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...

随机推荐

  1. struts2 基础demo1

    我们都知道 struts2 是基于webframework 出现的 优秀的mvc 框架, 他和struts1 完全没有联系.struts2 是一个框架, 啥叫框架呢?是一个优秀的半成品 . web的框 ...

  2. Tomcat内存溢出(java.lang.OutOfMemoryError: PermGen space)

    Tomcat启动时报如下错误:     java.lang.OutOfMemoryError: PermGen space 解决办法:     配置相关内存大小.其中按照启动tomcat的不同方式,分 ...

  3. jquery UI 弹出框

    2015-07-17 11:04:38 <div id="reg"></div> <script type="text/javascript ...

  4. selenium使用actions.moveToElement处理菜单

    //should set firefox path //FirefoxBinary binary=new FirefoxBinary(new File("C:\\Program Files ...

  5. MySQL表字段长度的限制

    在MySQL建表时,遇到一个奇怪的现象: root::>CREATE TABLE tb_test ( ) NOT NULL, ) DEFAULT NULL, ) DEFAULT NULL, ) ...

  6. css样式设置图片半透明度,兼容IE8,火狐

    关于背景颜色透明的兼容浏览器的问题,一直是个问题,我所写的兼容IE8,和火狐,说是兼容所有浏览器我就没有测试,有兴趣的朋友可以自己测试下吧. background-color:white;filter ...

  7. tableview详细介绍

    tableview详细介绍: https://www.baidu.com/link?url=MU5a5om66vYEKAcnvmXCeCwMGetezW5o2X11OUnwN7-fb_jWPx6xyv ...

  8. SQL语句删除重复数据

    1.如表中没有主键,先添加自动增长主键 alter table 表名 add 列名 int identity (1,1) primary key 2.删除重复数据 delete from 表名 whe ...

  9. Mac平台下Opencv开发环境搭建

    OpenCV(Open Source Computer Vision Library),是一个开源的跨平台的计算机视觉库,它实现了图像处理和计算机视觉领域的很多通用算法,可以在多种计算机平台上运行,支 ...

  10. NYOJ题目840吃花生

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAskAAAKdCAIAAABeSGNbAAAgAElEQVR4nO3dPXKkuvv28f8mnHshjn