hdu 5145(莫队算法+逆元)
NPY and girls
Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 790 Accepted Submission(s): 280
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.
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<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
using namespace std;
typedef long long LL;
const LL mod = ;
const int N = ;
int n,m;
LL a[N];
LL inv[N];
LL Hash[N];
LL res[N];
struct Node{
int l,r,sqr,id;
}node[N];
int cmp(Node a,Node b){
if(a.sqr==b.sqr) return a.r<b.r;
return a.l<b.l;
}
LL pow_mod(LL a,LL n){
LL ans = ;
while(n){
if(n&) ans = ans*a%mod;
a = a*a%mod;
n>>=;
}
return ans;
}
LL getinv(int x){
return pow_mod(x,mod-);
}
int main()
{
int tcase;
scanf("%d",&tcase);
for(int i=;i<=N;i++){
inv[i] = getinv(i);
}
while(tcase--)
{
scanf("%d%d",&n,&m);
int k = (int)sqrt(n);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
}
for(int i=;i<=m;i++){
scanf("%d%d",&node[i].l,&node[i].r);
node[i].id = i;
node[i].sqr = node[i].l/k;
}
sort(node+,node+m+,cmp);
memset(Hash,,sizeof(Hash));
int l=,r = ;
Hash[a[]]++;
LL ans = ;
for(int i=;i<=m;i++){
while(r<node[i].r){
r++;
Hash[a[r]]++;
ans = ans*(r-l+)%mod*inv[Hash[a[r]]]%mod;
}
while(l>node[i].l){
l--;
Hash[a[l]]++;
ans = ans*(r-l+)%mod*inv[Hash[a[l]]]%mod;
}
while(r>node[i].r){
ans = ans*Hash[a[r]]%mod*inv[r-l+]%mod;
Hash[a[r]]--;
r--;
}
while(l<node[i].l){
ans = ans*Hash[a[l]]%mod*inv[r-l+]%mod;
Hash[a[l]]--;
l++;
}
res[node[i].id] = ans;
}
for(int i=;i<=m;i++){
printf("%lld\n",res[i]);
}
}
return ;
}
hdu 5145(莫队算法+逆元)的更多相关文章
- HDU 4358 莫队算法+dfs序+离散化
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others)T ...
- HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = C ...
- Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元
题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...
- HDU 4638 莫队算法
Group Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 5145 NPY and girls(莫队算法+乘法逆元)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...
- HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)
传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...
- HDU 6278 - Just h-index - [莫队算法+树状数组+二分][2018JSCPC江苏省赛C题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6278 Time Limit: 6000/3000 MS (Java/Others) Memory Li ...
- HDU 3333 Turing Tree 莫队算法
题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...
- HDU 5381 The sum of gcd (技巧,莫队算法)
题意:有一个含n个元素的序列,接下来有q个询问区间,对每个询问区间输出其 f(L,R) 值. 思路: 天真单纯地以为是道超级水题,不管多少个询问,计算量顶多就是O(n2) ,就是暴力穷举每个区间,再直 ...
随机推荐
- 3226: [Sdoi2008]校门外的区间
链接 思路 bug漫天飞... 维护一颗线段树,支持区间赋值,和区间异或.因为会处理到一些方括号还是圆括号的问题,所以对于每一个下标都乘2,假设中间有一个.5即可,都变成了方括号,输出在处理一下. U ...
- 以+scheduledTimerWithTimeInterval...的方式触发的timer,在滑动页面上的列表时,timer会暂定回调,为什么?如何解决?
这里强调一点:在主线程中以+scheduledTimerWithTimeInterval...的方式触发的timer默认是运行在NSDefaultRunLoopMode模式下的,当滑动页面上的列表时, ...
- 孤荷凌寒自学python第四十七天通用跨数据库同一数据库中复制数据表函数
孤荷凌寒自学python第四十七天通用跨数据库同一数据库中复制数据表函数 (完整学习过程屏幕记录视频地址在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 今天打算完成的是通用的(至少目 ...
- (原)Unreal Shader模块(一): 着色创建
一.着色加载 这里说的Shader是编译后的文件或内存 源码说明 --------------------------------------------------------------- ...
- 线段树 (区间更新,区间查询) poj http://poj.org/problem?id=3468
题目链接 #include<iostream> #include<cstdio> #include<cmath> #include<cstdlib> # ...
- MySQL数据库有哪些特点
MySQL数据库的特点有: 它是C和C++语言编写的.支持多个操作系统.支持多线程.为多种编程语言提供API.优化SQL算法提高了查询速度以及提供用于管理和检查数据库的管理工具 MySQL数据库 My ...
- htmlagilitypack解析html
这是个很好的的东西,以前做Html解析都是在用htmlparser,用的虽然顺手,但解析速度较慢,碰巧今天找到了这个,就拿过来试,一切出乎意料,非常爽,推荐给各位使用. 下面是一些简单的使用技巧,希望 ...
- Linux networkmanager
我们开发的网络,出于保密,只能叫XXX网络,或者我更倾向于称之为WTF-network 由于经常处于封闭的环境,刚一接触新一点的世界,总是有那么一点猝不及防.最近发现配置的静态路由经常消失,经发现是n ...
- java 中基本类型与字符串之间的互相转换
1. 由 基本数据型态转换成 String String 类别中已经提供了将基本数据型态转换成 String 的 static 方法 也就是 String.valueOf() 这个参数多载的方法 有下 ...
- PB数据窗口中的几种状态及应用
数据窗口的状态主要有以下几种: 1)New! 2)NewModified! 3)DataModified! 4)NotModified! 数据窗口可以利用这些状态标志判断数据是否被修改过. 记录和字段 ...