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

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
 
Source
 
题意:有n个班级,询问在[l,r]区间内班级女生的排列方式有多少种,比如说 1 1 2,那么就有三种 1 1 2 ,2 1 1,1 2 1。
题解:首次接触莫队算法。
莫队算法:可以解决一类静态,离线区间查询问题。http://blog.csdn.net/bossup/article/details/39236275
发明这个的人好强啊,没看懂分块,不过用起来的话还是会用模板的.[l,r]里面的数字假设有m个,每个班级有c[i]个人,那么排列组合的总数为 m!/ ( c[1]! * .......c[n]! ) 关键是怎么用莫对算法求解这个式子。当区间从 [l,r-1] - >[l,r]时,那么假设之前的区间[l,r-1]里面的排列数为 N1,那么[l,r]的结果应当为 N1*(r+l-1)/c[a[r]++] ,反之,当现在的区间从
[l,r+1]->[l,r]时,假设 [l,r+1]里面的排列数为 N2,那么当前[l,r]里面的排列数应该变成了 N2*c[a[r+1]++]/(r-l+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(莫队算法+逆元)的更多相关文章

  1. HDU 4358 莫队算法+dfs序+离散化

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others)T ...

  2. HDOJ:6333-Problem B. Harvest of Apples(组合数学+莫队算法+逆元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 解题心得: 这个题可以说是十分精彩了,首先推组合数学的公式,其中一个很重要的公式是Cnm = C ...

  3. Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元

    题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...

  4. HDU 4638 莫队算法

    Group Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  5. HDU 5145 NPY and girls(莫队算法+乘法逆元)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=5145 [题目大意] 给出一个数列,每次求一个区间数字的非重排列数量.答案对1e9+7取模. [题解 ...

  6. HDU5145:5145 ( NPY and girls ) (莫队算法+排列组合+逆元)

    传送门 题意 给出n个数,m次访问,每次询问[L,R]的数有多少种排列 分析 \(n,m<=30000\),我们采用莫队算法,关键在于区间如何\(O(1)\)转移,由排列组合知识得到,如果加入一 ...

  7. 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 ...

  8. HDU 3333 Turing Tree 莫队算法

    题意: 给出一个序列和若干次询问,每次询问一个子序列去重后的所有元素之和. 分析: 先将序列离散化,然后离线处理所有询问. 用莫队算法维护每个数出现的次数,就可以一边移动区间一边维护不同元素之和. # ...

  9. HDU 5381 The sum of gcd (技巧,莫队算法)

    题意:有一个含n个元素的序列,接下来有q个询问区间,对每个询问区间输出其 f(L,R) 值. 思路: 天真单纯地以为是道超级水题,不管多少个询问,计算量顶多就是O(n2) ,就是暴力穷举每个区间,再直 ...

随机推荐

  1. Struts2---数据封装机制

    Struts2属性驱动和模型驱动 自动完成了数据的获取和封装 LoginAction.java public class LoginAction implements ModelDriven<U ...

  2. Android 快捷方式的创建与查询 快捷方式问题大全 获取快捷方式在Launcher数据库中的信息 Failed to find provider info for com.android.la

    /** * 创建添加快捷方式 * 其中需要设置的有: * 1. 快捷方式的标题 * 2. 快捷方式的图标 * 3. 点击快捷方式后的跳转 */ public static void createSho ...

  3. stm32--free modbus 1.5.0移植(作为从机)

    添加文件 获取原始free modbus library(官网) 将...\freemodbus-v1.5.0\demo\BARE中的所有文件复制到...\freemodbus-v1.5.0\modb ...

  4. Java算法求最大最小值,倒序,冒泡排序,斐波纳契数列,日历一些经典算法

    一,求最大,最小值 int[] a={21,31,4,2,766,345,2,34}; //这里防止数组中有负数,所以初始化的时候给的数组中的第一个数. int max=a[0]; int min=a ...

  5. python学习总结 --函数基础

    函数基础 ### 函数简介 - 定义:具有特定功能的一段代码 - 优点: - 可以减少代码的重复书写 - 可以将功能的实现着和使用者分开,可以提高开发效率 - 分类: - 库函数:print.inpu ...

  6. 孤荷凌寒自学python第五十一天初次尝试使用python连接Firebase数据库

    孤荷凌寒自学python第五十一天初次尝试使用python连接Firebase数据库 (完整学习过程屏幕记录视频地址在文末) 今天继续研究Firebase数据库,利用google免费提供的这个数据库服 ...

  7. Spring MVC表单标签

    从Spring 2.0开始,Spring MVC开始全面支持表单标签,通过Spring MVC表单标签,我们可以很容易地将控制器相关的表单对象绑定到HTML表单元素中. form标签     和使用任 ...

  8. 想了一天的题目QAQ 毛线数列的最值

    #include <cstdio> #include <cstring> #include <cmath> #include <iostream> #i ...

  9. PHP面向对象练习

    练习内容:随机生成一个字符串 代码: <?phpclass randstring{ private $length; private $type; private $one = array(0, ...

  10. 当网卡收到一个包的目的地址是本主机其他接口的IP时.2

    arp包进入主机后要经过的过滤是:rp_filter rp_filter会过滤网段 所以说不要在进行arp_ignore测试的时候把rp_filter设置成2, 此时就不会对源地址进行路由的检查了 然 ...