NPY and girls-HDU5145莫队算法
| Time Limit: | 8000/4000 MS (Java/Others) |
|---|---|
| Memory Limit: | 32768/32768 K (Java/Others) |
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
莫队算法处理区间查询问题,对于[L,R]区间中的组合所有的情况是(R-L+!)!/(num[i]!num[j]!….),所以可以根据[L,R]的情况来推其他的情况。
#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
const int Max = 31000;
const int Mod =1e9+7;
typedef struct node
{
int l,r,Id;
int L;
bool operator < (const node &a)const
{
return L<a.L||(L==a.L&&r<a.r);
}
}Node;
int a[Max];
Node s[Max];
LL ans[Max];
LL Inv[Max];
int num[Max];
LL Pow(LL n,LL m)
{
LL ans = 1;
while(m)
{
if(m&1)
{
ans = (ans*n)%Mod;
}
n = (n*n)%Mod;
m>>=1;
}
return ans;
}
void Init() // 求逆元
{
for(int i = 0;i<=30000;i++)
{
Inv[i] = Pow(i,Mod-2);
}
}
int main()
{
Init();
int T;
int n,m;
scanf("%d",&T);
while(T--)
{
memset(num,0,sizeof(num));
scanf("%d %d",&n,&m);
int k = sqrt(n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for(int i=0;i<m;i++)
{
scanf("%d %d",&s[i].l,&s[i].r);
s[i].L = s[i].l/k;
s[i].Id = i;
}
sort(s,s+m);
LL l=1,r=1;
num[a[1]]++;
LL res = 1;
for(int i=0;i<m;i++)
{
while(r<s[i].r)
{
r++;
num[a[r]]++;
res = (((r-l+1)*res)%Mod*Inv[num[a[r]]])%Mod;
}
while(r>s[i].r)
{
res = ((res*num[a[r]])%Mod*Inv[r-l+1])%Mod;
num[a[r]]--;
r--;
}
while(l>s[i].l)
{
l--;
num[a[l]]++;
res = (((r-l+1)*res)%Mod*Inv[num[a[l]]])%Mod;
}
while(l<s[i].l)
{
res = ((res*num[a[l]])%Mod*Inv[r-l+1])%Mod;
num[a[l]]--;
l++;
}
ans[s[i].Id] = res;
}
for(int i=0;i<m;i++)
{
printf("%lld\n",ans[i]);
}
}
return 0;
}
NPY and girls-HDU5145莫队算法的更多相关文章
- HDU 5145 NPY and girls(莫队算法+乘法逆元)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...
- HDU 5145 NPY and girls (莫队分块离线)
题目地址:HDU 5145 莫队真的好奇妙.. 这种复杂度竟然仅仅有n*sqrt(n)... 裸的莫队分块,先离线.然后按左端点分块,按块数作为第一关键字排序.然后按r值作为第二关键字进行排序. 都是 ...
- hdu5145 莫队算法
这题说的是个了n个数字 然后 在L 和R 区间内的数字的排列有多少种方案, 这里我们通过 将 这n长度的字符串 分成sqrt(n) 块然后 一个属性 他们的l 属于 那个快 以这个为第一关键字 ,然 ...
- HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)
传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...
- hdu 5145(莫队算法+逆元)
NPY and girls Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- NBUT 1457 莫队算法 离散化
Sona Time Limit:5000MS Memory Limit:65535KB 64bit IO Format: Submit Status Practice NBUT 145 ...
- BZOJ 2038: [2009国家集训队]小Z的袜子(hose) [莫队算法]【学习笔记】
2038: [2009国家集训队]小Z的袜子(hose) Time Limit: 20 Sec Memory Limit: 259 MBSubmit: 7687 Solved: 3516[Subm ...
- Codeforces617 E . XOR and Favorite Number(莫队算法)
XOR and Favorite Number time limit per test: 4 seconds memory limit per test: 256 megabytes input: s ...
- Bzoj 2038---[2009国家集训队]小Z的袜子(hose) 莫队算法
题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=2038 Description 作为一个生活散漫的人,小Z每天早上都要耗费很久从一堆五颜六色 ...
随机推荐
- 带你玩转JavaWeb开发之六-mysql基本语法详解及实例(1)
1.1.1 对数据库的表进行操作 1.1.1.1 对数据库中表进行创建 [语法:] create table 表名( 列名 列类型 [列约束], 列名 列类型 [列约束], 列名 列类型 [ ...
- mvc配合jquery.validate验证失效,情况之一
用viewbage绑定input空间的value值,通过submit提交.validate验证失效. 1.应该是mvc的渲染顺序导致js验证失败. 解决方案:改用mvc自带的@html辅助方法,生成文 ...
- gvim初学命令记录
一.vim进入和退出(在正常模式下进行)若不能保证是否处于正常模式,先按下ESC键不保存退出 :q!(冒号也是键的)保存退出 :wq二.移动 k(上)h(左) l(右) j(下)三.删除(可类似于剪切 ...
- javascript的缓动效果
这部分对原先的缓动函数进行抽象化,并结合缓动公式进行强化.成品的效果非常惊人逆天.走过路过不要错过. 好了,打诨到此为止.普通的加速减速是难以让人满意的,为了实现弹簧等让人眼花缭乱的效果必须动用缓动公 ...
- c语言第11次作业
#include<stdio.h> void sum(int *p,int n) { ]; ; int i; ;i<n;++i) { sum+=*p+a[i]; } } int ma ...
- Java Timer定时器时,每次重复执行了两次任务的解决方案
web.xml监听配置 com.numenzq.mc.service.impl.TimerListener TimerListener类 public class TimerListener impl ...
- CSS3外轮廓属性
外轮廓outline在页面中呈现的效果和边框border呈现的效果极其相似,但和元素边框border完全不同,外轮廓线不占用网页布局空间,不一定是矩形,外轮廓是属于一种动态样式,只有元素获取到焦点或者 ...
- sdk、jdk、jre、jvm、jdt、cdt分别都是什么东西
1.sdk是什么 sdk,全称是 software development kit 中文译为软件开发工具包.kit 在英文中有工具箱,设备包,成套工具的意思. 从名字就可以看出sdk作为一个工具箱,其 ...
- Log(android.util.Log)(option+return)
Log.v() verbose 琐碎,详细 Log.d() debug 调试 Log.i() info 信息,重要,分析行为 Log.w() wain 警告 log.e() error 错误 参数:t ...
- 控制反转Inversion of Control (IoC) 与 依赖注入Dependency Injection (DI)
控制反转和依赖注入 控制反转和依赖注入是两个密不可分的方法用来分离你应用程序中的依赖性.控制反转Inversion of Control (IoC) 意味着一个对象不会新创建一个对象并依赖着它来完成工 ...