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. SpringBootTest 测试工具

    以下内容,翻译自官方文档,并结合了学习过程的demo. Spring Boot提供了许多实用程序和注解,帮助测试应用程序.测试支持由两个模块提供:spring-boot-test 包含核心项,spri ...

  2. day5 函数和参数

    函数就是最基本的一种代码抽象的方式 定义一个函数使用def语句 def my_abs(x): if x >= 0: return x else: return -x 定义一个什么事也不做的空函数 ...

  3. 【DPDK】【CPU usage】DPDK应用如何计算当前系统的压力

    [前言] 使用DPDK开发的朋友应该都了解使用dpdk的fwd线程的工作模式是polling模式,即100%轮询的方式去加速网络IO,这样我们在操作系统层面上来观察目标processer会发现usag ...

  4. 使用gravatar生成头像

    avatar代表您在线的图像,当你与网站互动时,你的名字旁边会出现一张图片. Gravatar是一个全球通用的头像.你只需上传一次并创建自己的个人资料,然后当你加入任何支持Gravatar的网站时,你 ...

  5. nacos-docker安装nacos并配置数据库

    拉取nacos/nacos-server镜像 docker pull nacos/nacos-server 配置数据库(MySQL) 创建存储nacos配置的数据库 create database n ...

  6. 大白话原型模式(Prototype Pattern)

    意图 原型模式是创建型设计模式,可以复制已存在的对象而无需依赖它的类. 问题 假如现在有一个对象,我们想完全复制一份新的,我们该如何做? 创建同一个类的新对象 遍历所有已存在对象的值,然后将他们的值复 ...

  7. 村庄之间建立邮局 - 区间 dp

    There is a straight highway with villages alongside the highway. The highway is represented as an in ...

  8. Java知识体系框架

    前言:自从出生,每个人都是一个学习者或探索者.永远保持一颗谦逊的心态,遵循一定的方法和规范,去学习和实践,永远记得走走停停,多回头看看自己走过的路,温故而知新,也能更好地指导未来的路怎么走(同样,本篇 ...

  9. hdu6703 线段树+set

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6703 给你一个数组两种操作.操作一是将pos位置的数字加上10000000:操作二是给你个r和k,问你 ...

  10. Scrapy信号量

    1.类 from scrapy import signals class MySingle(object): def __init__(self): pass @classmethod def fro ...