人生中第一道莫队,本来以为是一道水题的。。

首先这题只有区间查询,没有修改操作,使用莫队比较明显,但统计答案有点麻烦。。

根据题意,在n个人里选m个不相同种类的人,设第i种人数量为ai,总方案为c(n,a1)*c(n-a1,a2)*c(n-a1-a2,a3)*...*c(n-a1-a2-...-an-1,an)。而我们要用O(1)的复杂度实现转移,那么我们可以用c(n,m)=c(n-1,m-1)*n/m来实现转移,在模意义下除法必须使用逆元,那么我们利用费马小定理预处理出每个值的逆元就好了。。

总的来说,莫队算法真的很神啊。。不仅思想简单,实现简单,复杂度还很优秀。。

//It is made by wfj_2048~
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#define inf 1<<30
#define il inline
#define RG register
#define ll long long
#define eps 1e-9
#define rhl 1000000007LL
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout) using namespace std; struct node{ int l,r,id,bl; }p[30010]; int a[30010],c[30010],n,m;
ll inv[30010],ans[30010]; il int gi(){
RG int x=0,q=0; RG char ch=getchar();
while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=1,ch=getchar();
while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;
} il int cmpt(const node &a,const node &b){ return a.bl<b.bl || (a.bl==b.bl && a.r<b.r); } il ll q_pow(RG ll a,RG ll b){ RG ll ans=1,x=a; while (b){ if (b&1) ans=ans*x%rhl; x=x*x%rhl,b>>=1; } return ans; } il void solve(){
RG int l=1,r=0; RG ll Ans=1;
for (RG int i=1;i<=m;++i){
while (l>p[i].l){ l--,c[a[l]]++,Ans=Ans*(ll)(r-l+1)%rhl*inv[c[a[l]]]%rhl; }
while (r<p[i].r){ r++,c[a[r]]++,Ans=Ans*(ll)(r-l+1)%rhl*inv[c[a[r]]]%rhl; }
while (l<p[i].l){ Ans=Ans*(ll)c[a[l]]%rhl*inv[r-l+1]%rhl,c[a[l]]--,l++; }
while (p[i].r<r){ Ans=Ans*(ll)c[a[r]]%rhl*inv[r-l+1]%rhl,c[a[r]]--,r--; }
ans[p[i].id]=Ans;
}
return;
} il void work(){
n=gi(),m=gi(); RG int block=sqrt(n*1.0); for (RG int i=1;i<=n;++i) a[i]=gi(),c[i]=0,ans[i]=0;
for (RG int i=1;i<=m;++i) p[i].l=gi(),p[i].r=gi(),p[i].id=i,p[i].bl=(p[i].l-1)/block+1;
sort(p+1,p+m+1,cmpt); solve(); for (RG int i=1;i<=m;++i) printf("%lld\n",ans[i]); return;
} int main(){
File("npy");
for (RG int i=1;i<=30000;++i) inv[i]=q_pow((ll)i,(ll)rhl-2);
RG int T=gi(); while (T--) work();
return 0;
}

hdu5145 NPY and girls的更多相关文章

  1. HDU 5145 NPY and girls 莫队+逆元

    NPY and girls Problem Description NPY's girlfriend blew him out!His honey doesn't love him any more! ...

  2. NPY and girls

    NPY and girls 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5145 莫队算法 注意到没有修改区间的操作,使用莫队算法:将整个区间分成若干 ...

  3. 【HDU 5145】 NPY and girls(组合+莫队)

    pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...

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

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

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

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

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

  7. HDU 5145 NPY and girls (莫队分块离线)

    题目地址:HDU 5145 莫队真的好奇妙.. 这种复杂度竟然仅仅有n*sqrt(n)... 裸的莫队分块,先离线.然后按左端点分块,按块数作为第一关键字排序.然后按r值作为第二关键字进行排序. 都是 ...

  8. hdu 5145(莫队算法+逆元)

    NPY and girls Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  9. NPY and girls-HDU5145莫队算法

    Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Description ...

随机推荐

  1. $_FILES详解

    <form enctype="multipart/form-data" action="upload.php" method="post&quo ...

  2. JVM中GC浅解:垃圾回收的了解

    1.为什么要有GC 没有GC的世界,我们需要手动进行内存管理,但是内存管理是纯技术活,又容易出错.但是我们写码的目的是为了解决业务问题,所以可以把这种纯技术活自动化,当然自动化也是有代价的. 2.垃圾 ...

  3. 分享几个不错的Android开源音视频播放器

    整理了一下Github上几个开源的音视频播放器项目,有兴趣的同学可以clone代码去研究学习.   UniversalMusicPlayer https://github.com/googlesamp ...

  4. DLL:无法解析的外部符号 "__declspec(dllimport)

    (4.10)碰到问题:MFC规则DLL(静态库1)中,AFX_EXT_CLASS导出类及类的对象.在静态库2中使用.出现两个问题:  (1) 静态库1中警告:dll链接不一致;  (2) 静态库2中报 ...

  5. git使用教程及github远程仓库管理

    git使用教程步骤 1.安装git 安装完后,在开始菜单里找到"git"->"Git Bash",蹦出一个类似命令行窗口,说明安装成功.2 注册git 在 ...

  6. iOS开发之UINavigationController

    1.概述: 利用UINavigationController,可以轻松地管理多个控制器,轻松完成控制器之间的切换,典型例子就是系统自带的“设置”应用. 2.UINavigationController ...

  7. css gray,grayscale,css变灰兼容大部分浏览器

    css gray,grayscale,css变灰兼容大部分浏览器 html { filter: url("data:image/svg+xml;utf8,<svg xmlns=\'ht ...

  8. WIMP环境搭建

    h1 { color: #3366ff } p { font-size: 16px } body { background-color: rgb(200,200,169) } 环境说明 系统:wind ...

  9. sui的一些方法封装

    + function($) { "use strict"; var today = new Date(); var getDays = function(max) { var da ...

  10. Java并发编程:同步锁、读写锁

    之前我们说过线程安全问题可以用锁机制来解决,即线程必要要先获得锁,之后才能进行其他操作.其实在 Java 的 API 中有这样一些锁类可以提供给我们使用,与其他对象作为锁相比,它们具有更强大的功能. ...