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. adb 服务端口2037被占,导致adb和appium无法工作

    症状1: 命令行运行 adb 相关命令,提示如下: adb server is out of date. killing...ADB server didn't ACK* failed to star ...

  2. Zookeeper服务常用的操作命令

    Zookeeper服务安装之后,一般会在这个服务的基础之上安装其他的大数据平台,其他的框架一般会提供很多接口对Zookeeper中的内容进行一定的操作,但是功能相对单一,所以有些时候,有必要我们自己登 ...

  3. 和我一起学python,基本概念 (life is short ,we need python)

    作者:tobecrazy  出处:http://www.cnblogs.com/tobecrazy 欢迎转载,转载请注明出处.thank you! 基本概念 : 常量: 常量名全部大写,如PI 变量: ...

  4. ffmpeg-20160517-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  5. 15.SpringMVC和Spring上下文关系(为什么SpringMVC可以调用到Spring)

    springmvc上下文继承于spring, 也就是springmvc的上下文可访问spring上下文,在springmvc的上下文中可取得spring bean. spring上下文是spring启 ...

  6. 一道常考fork题挖掘

    #include <stdio.h> #include <sys/types.h> #include <unistd.h> int main(void) { int ...

  7. 用window.print()打印指定div里面的内容(转载的)

    用window.print()打印指定div里面的内容 今天客户让添加个打印证照功能,直接用window.print()打印的是整个页面,而用以下方法就可以只打印证明了 <!--window.p ...

  8. 【XLL API 函数】 xlFree

    用于释放使用 Excel4,Excel4v,Excel12,Excel12v 分配的 XLOPER/XLOPER12 占用的内存资源. xlFree 函数释放辅助内存和重置指针为NULL但不释放XLO ...

  9. cocos2d-x 第一篇 环境搭建

    官网:http://www.cocos2d-x.org/ 下载一个稳定版的cocos2d-x (网址:http://download.cocos2d-x.org/ Github Repository ...

  10. py随笔

    while true,无限循环 str.isdigit判断是不是数字 +只能在两个两个相同的类型之间执行 iter(i)将i加入迭代器