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 ...
随机推荐
- POJ 1062 ( dijkstra )
http://poj.org/problem?id=1062 一个中文题,一个多月之前我做过,当时我是用搜索写的,不过苦于卡在无法确定等级关系,所以就错了. 看了别人的博客后,我还是不是很理解所谓的枚 ...
- JS中的timestamp
http://blog.163.com/lijy_980720@126/blog/static/75574626201261783343834/
- 抓取网页内容生成kindle电子书
参考: http://calibre-ebook.com/download_linux http://blog.codinglabs.org/articles/convert-html-to-kind ...
- c#缓存 笔记
1:缓存. 你需要了解大数据高并发的瓶颈在哪里,一般都是数据库层面的,机械硬盘承载不起非常快速的读写操作,cpu承载不起大量的逻辑运算,所以最基本的解决思路就是:1.换固态硬盘加快硬盘的读写效率.2. ...
- ACM/ICPC 之 DP-整数划分问题初探 (POJ1221)
写下这道题的原因很简单= =,因为这一题的状态转移方程不好找,另一方面,我看到很多针对这一题写的解题报告都把累加状态说得模棱两可,甚至直接说成了一个单一状态,弄得本是菜鸟的我硬生生折磨了一上午画了几个 ...
- monitor disk
#!/bin/bash # #top #Big_USERS - find big disk space users in various directories ################### ...
- 后台弹出JS类
using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...
- 配置TFS2010的用户截图
先要添加一个管理用户
- -bash: pod: command not found
OS X 系统没升级之前用的 cocoapods 一点儿问题都没有,但是升级成版本10.11.4 OS X EI Capitan之后,在终端除了cd 指令可以用之外,其他任何指令输入都是提示-bash ...
- [Android Pro] Android下toolbox简介
toolbox是Android 自带的提供shell命令的软件.有点类似于busybox,但功能上好像弱很多.其源码可以从Android source code 中system/core/toolbo ...