http://acm.hdu.edu.cn/showproblem.php?pid=5145

初探莫队,就是离线排序后的暴力,但是效率大大提高,中间要除法取模,所以用到了逆元。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define LL long long
#define MOD 1000000007
using namespace std; int a[],num[],n,m,sizee;
LL ans[],inv[];
struct node
{
int l,r,num,belong;
}query[]; void init()
{
for(int i = ;i <= ;i++)
{
LL temp = ,x = i;
int pow = MOD-;
while(pow)
{
if(pow%) temp = temp*x%MOD;
x = x*x%MOD;
pow /= ;
}
inv[i] = temp;
}
} int cmp(node x,node y)
{
if(x.belong == y.belong) return x.r < y.r;
return x.l<y.l;
} int main()
{
init();
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
sizee = sqrt(n);
for(int i = ;i <= n;i++) scanf("%d",&a[i]);
for(int i = ;i <= m;i++)
{
scanf("%d%d",&query[i].l,&query[i].r);
query[i].num = i;
query[i].belong = (query[i].l-)/sizee+;
}
sort(query+,query++m,cmp);
memset(num,,sizeof(num));
int ll = ,rr = ;
LL now = ;
num[a[]]++;
for(int i = ;i <= m;i++)
{
while(rr < query[i].r)
{
rr++;
num[a[rr]]++;
now = now*(rr-ll+)%MOD*inv[num[a[rr]]]%MOD;
}
while(ll > query[i].l)
{
ll--;
num[a[ll]]++;
now = now*(rr-ll+)%MOD*inv[num[a[ll]]]%MOD;
}
while(ll < query[i].l)
{
now = now*num[a[ll]]%MOD*inv[rr-ll+]%MOD;
num[a[ll]]--;
ll++;
}
while(rr > query[i].r)
{
now = now*num[a[rr]]%MOD*inv[rr-ll+]%MOD;
num[a[rr]]--;
rr--;
}
ans[query[i].num] = now;
}
for(int i = ;i <= m;i++) printf("%lld\n",ans[i]);
}
return ;
}

HDU_1175_莫队+逆元的更多相关文章

  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. 吉首大学2019年程序设计竞赛(重现赛)I 滑稽树上滑稽果 (莫队+逆元打表)

    链接:https://ac.nowcoder.com/acm/contest/992/I来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 32768K,其他语言65536K  ...

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

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

  4. 2018 Multi-University Training Contest 4 Problem B. Harvest of Apples 【莫队+排列组合+逆元预处理技巧】

    任意门:http://acm.hdu.edu.cn/showproblem.php?pid=6333 Problem B. Harvest of Apples Time Limit: 4000/200 ...

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

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

  6. Problem B. Harvest of Apples(杭电2018年多校+组合数+逆元+莫队)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6333 题目: 题意:求C(n,0)+C(n,1)+……+C(n,m)的值. 思路:由于t和n数值范围太 ...

  7. HDU 6333.Problem B. Harvest of Apples-组合数C(n,0)到C(n,m)求和-组合数学(逆元)+莫队 ((2018 Multi-University Training Contest 4 1002))

    2018 Multi-University Training Contest 4 6333.Problem B. Harvest of Apples 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...

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

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

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

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

随机推荐

  1. vue入门,vue指令,vue组件,vue模板

    vue 使用虚拟dom操作减少真实dom操作 提高页面的渲染效率 虚拟dom的本质就是内存中的一个对象,该对象和dom结构相互对应 将开发者经历从dom中释放出来,转移到数据的操作 开发者不需要关注页 ...

  2. VS从标准输入读入文件

    1.点击[生成],在对应目标平台[64 or 32]文件夹下的[release]或[debug]下找到可执行文件 2.读取销售记录文件 1)打开cmd,将销售记录文件和可执行文件放在同一文件夹下 2) ...

  3. Freemarker 的基础使用 (一)

    以下内容来自:http://www.oschina.net/p/freemarker FreeMarker是一个模板引擎,一个基于模板生成文本输出的通用工具,使用纯Java编写. FreeMarker ...

  4. pdf文件内容查看器 -- 采用wpf开发

    前言 pdf是一种应用非常广的版式文档格式,已成为事实上的国际标准.关于pdf格式的文章汗牛充栋,本文也是关于pdf格式的文章,但是本文不是纸上谈兵:本人这几周一直研究pdf格式内容,不但对pfd格式 ...

  5. curl使用post方式访问Spring Cloud gateway报time out错误

    公司老的项目使用是php,要进行重构.其他团队使用php curl函数使用post方式调用Spring Cloud gateway 报time out错误. 但是使用postman测试是没有任何问题, ...

  6. Redis系列(一):Redis简介及环境安装

    提到Redis,大家肯定都听过,并且应该都在项目中或多或少的使用过,也许你觉得Redis用起来挺简单的呀,但如果有人问你下面的几个问题(比如同事或者面试官),你能回答的上来吗? 什么是Redis? R ...

  7. WIN10升级后输入法无法输入中文

    查看是否安装了中文输入法,可能在升级后用户文件出现问题. 在设置>语言.添加一下中文输入法.

  8. webpack 三

    之前06年写了两篇自己研究webpack的文章,由于webpack已经升到了4.x,今天用起来发现有点小变化,重新自己建一个简单的项目,这里记录一下过程 1.安装webpack和webpack-cli ...

  9. uni-app,vue,react,Trao之缓存类封装

    uni-app,vue,react,Trao之缓存类封装 一,介绍与需求 1.1,介绍 缓存主要分为如下几个 1.LocalStorage LocalStorage是永久性的本地缓存,存储在客户端的浏 ...

  10. 怎么将文件夹上传到GitHub上

    1. 在GitHub上新建一个仓库地址: http://github.com/......git 2. 在需要上传的文件夹目录下,运行 git   init  初始化git: 3. 运行git  ad ...