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. json和对象、list互转

    1.把对象,list转成json: import com.alibaba.fastjson.JSONObject; Object obj = new Object(); // ... String o ...

  2. Unity3d NavMesh获得地面高度

    UnityPro内置的NavMesh有几个API很有用 NavMesh.SamplePosition 根据给的点进行采样,可传入最大距离,返回true说明采样到了点,否则采样失败(可以用来获得地形高度 ...

  3. ios The App Life Cycle

    先推荐ios 必读文章 App Programming Guide for iOS ,请在苹果官网搜索,并仔细阅读所有内容 State Description Not running The app ...

  4. linux内置的审计跟踪工具------last和lastb

    last是linux的一个内置工具,可以用来查看最后登录服务器的用户.它对于追踪非常有用. last命令显示的是来自/var/log/wtmp文件创建起,所有登录的用户.这个文件是一个二进制文件,不能 ...

  5. socket端口重复占用问题

    1.一个服务端进程在主动释放端口后(调用close)端口状态为TIME_WAIT,这时再去监听同样的端口,不论是否设置SO_REUSEADDR,都能监听成功,也能接收到客户端的连接,但是无法收到数据. ...

  6. [Linux]yum开启rpm包缓存

    在CentOS下用yum安装,回发现在/var/cache/yum/下的base.extrs和updates下的packages下都没有发现下载的RPM 原来在/etc/yum.conf下没有设置下载 ...

  7. 用 get 同步/异步 方式获取网络数据并输出

    //同步请求 //创建NSString用来存储请求的网址 NSString* str=@"http://v.juhe.cn/weather/index?format=2&cityna ...

  8. 前端easyui的简化调用

    easyui近期一直都比较流行,虽然它在效果上被extjs爆了,它的使用难度低,在IE6下表现不错,的确受到了广泛企业程序员的好评. 但是他的API说明还是比较简陋的,刚上手可能还需要摸索一下,为什么 ...

  9. 史上最全web.xml配置文件元素详解

    一.web.xml配置文件常用元素及其意义预览 <web-app> <!--定义了WEB应用的名字--> <display-name></display-na ...

  10. Maven Java EE Configuration Problem 的完美解决办法

    背景: 最近在修改项目的时候,发现修改了项目依赖以后会出现如下图:Maven Java EE Configuration Problem 的问题,对于有强迫症的我来说,看到项目上面有个很小的红色小叉号 ...