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. echart图表展示数据-简单的柱状图

    话不多说,先上几张效果图 给大家看看 1:echart所用到的文件包需要事先引入好具体可见 http://echarts.baidu.com/doc/start.html 2:本例中所有的数据都是通过 ...

  2. erlang连接mysql [转]

    转自: http://blog.csdn.net/flyinmind/article/details/7740540 项目中用到erlang,同时也用到mysql.惯例,google. 但是,按照网上 ...

  3. NSThread那些事儿

    NSThread 哎呀,它面向对象,再去看看苹果提供的API,对比一下Pthreads,简单明了,人生仿佛又充满了阳光和希望,我们先来一看一下系统提供给我们的API自然就知道怎么用了,来来来,我给你注 ...

  4. 《Cracking the Coding Interview》——第5章:位操作——题目3

    2014-03-19 05:57 题目:给定一个整数N,求出比N大,而且二进制表示中和N有相同个数的‘1’的最小的数,比如3是‘11’,接下来的5是‘101’,再接下来的6是‘110’. 解法:从低位 ...

  5. USACO Section1.2 Transformations 解题报告

    transform解题报告 —— icedream61 博客园(转载请注明出处)------------------------------------------------------------ ...

  6. jmeter学习(二),如何安装jmeter?

    官网地址:http://jmeter.apache.org/download_jmeter.cgi 如下图数字3.2表示的是版本号,jmeter是基于java的压力测试工具.所以运行环境一定要满足最低 ...

  7. ASP.net MVC入门及Razor语法

    一.MVC入门: 1.MVC简介 约定大于配置 2.MVC访问流程 csthml模板(razor模板)就是简化HTML的拼接的模板,最终还是生成html给浏览器显示,不能直接访问cshtml文件. 二 ...

  8. Linux特殊权限位

    SUID 运行某程序时,相应进程的属主是程序文件自身的属主,而不是启动者(启动者临时获得文件属主的权限)     chmod u+s file     chmod u-s file SGID 运行某程 ...

  9. JavaWeb笔记(七)Filter&Listener

    Filter 实现Filter接口 一般用于完成通用的操作,如:登陆验证.统一编码处理.敏感字符过滤等 执行流程 执行过滤器 执行放行后的资源 继续执行过滤器放行代码下的代码 配置 拦截路径配置 注解 ...

  10. Eureka 向Server中注册服务

    Eureka支持注册与发现服务,本章讲解如何像服务中心注册服务. 在父工程下创建EurekaClient工程(eureka-provider): pom.xml <?xml version=&q ...