HDU 5145 NPY and girls 莫队+逆元
NPY and girls
Problem Description
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.
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.
4 2
1 2 1 3
1 3
1 4
1 1
1
1 1
12
1
#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 莫队+逆元的更多相关文章
- HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)
传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...
- 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值作为第二关键字进行排序. 都是 ...
- 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 <= ...
- NPY and girls-HDU5145莫队算法
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...
- hdu_5145_NPY and girls(莫队算法+组合)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5145 题意:给你n,m,共有n个女孩,标号为1—n,n个数xi表示第ith个女孩在第xi个教室,然后下 ...
- HDU_1175_莫队+逆元
http://acm.hdu.edu.cn/showproblem.php?pid=5145 初探莫队,就是离线排序后的暴力,但是效率大大提高,中间要除法取模,所以用到了逆元. #include< ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- hdu 4676 Sum Of Gcd 莫队+phi反演
Sum Of Gcd 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=4676 Description Given you a sequence of ...
随机推荐
- IPC----哲学家就餐问题(并发与互斥)
哲学家就餐问题描述: 5个哲学家,5个筷子.5个哲学家围坐在一张桌子上,筷子放在分别放在每个哲学家的两旁.如果所有哲学家在某个时刻同时拿起左边的筷子,那么右边的筷子就都被其他的哲学家拿了,造成大家都无 ...
- centos7.0改变用户创建目录组权限
centos7.0改变用户创建目录组权限可通过umask进行设置. 临时改变可通过umask命令进行设置 永久性改变,可通过修改~/.bash_profile的方式进行调整.
- hdu2196
基本的树形dp,需要dfs三次,第一次求每个点最远的后代,第二次和第三次每个点的孩子分别从左到右和从右到左遍历. #include <cstdio> #include <vector ...
- 手动设置Windows7锁屏界面背景
windows7系统安装之后锁屏.关机界面.开机欢迎界面都是系统默认的背景,其实这些背景就像桌面壁纸一样是可以更改的,如果没有修改过的话,按下面步骤就可以修改了. 首先选择一张喜欢的背景图片,分辨率不 ...
- FFmpeg-20160422-snapshot-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 F ...
- FFmpeg-20160418-snapshot-bin
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 F ...
- 3.SpringMVC修改配置文件路径和给界面传递数据
1.修改配置文件路径 达到 配置多文件的目的 web.xml文件中基础配置有springMVC配置的servlet路径 <servlet-name>SpringMVC</serv ...
- SQL中关于字符串的处理
DECLARE @STR VARCHAR(MAX),@PRAM VARCHAR(20)SET @STR='中华人民共和国万岁'--字符串连接SET @STR=@STR+'an'+@STR --字符串拆 ...
- Effective C++ -----条款12: 复制对象时勿忘其每一个成分
Copying函数应该确保复制“对象内的所有成员变量”及“所有base class成分”. 不要尝试以某个copying函数实现另一个copying函数.应该将共同机能放进第三个函数中,并由两个cop ...
- linux下QT Creator常见错误及解决办法
最近因为在做一个关于linux下计算机取证的小项目,需要写一个图形界面,所以想到了用QT来写,选用了linux下的集成开发环境QT Creator5.5.1,但刚刚安装好,竟然连一个"hel ...