HDU - 6333:Harvest of Apples (组合数前缀和&莫队)
Count the number of ways to pick at most m m
apples.
InputThe first line of the input contains an integer T T
(1≤T≤10 5 ) (1≤T≤105)
denoting the number of test cases.
Each test case consists of one line with two integers n,m n,m
(1≤m≤n≤10 5 ) (1≤m≤n≤105)
.
OutputFor each test case, print an integer representing the number of ways modulo 10 9 +7 109+7
.Sample Input
2
5 2
1000 500
Sample Output
16
924129523
题意:T组询问,每次给出N, M,求C(N,0)+C(N,1)+...C(N,M);
思路:前缀和没有什么特别的公式, 所以我们考虑询问之间的关系:
易得,F(N,M)=F(N,M-1)+C(N,M);
由杨辉三角,可得, F(N,M)=2*F(N-1,M)-C(N-1,M);
然后就可以跑莫队了.
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int Mod=1e9+;
int f[maxn],rev[maxn],ans[maxn],B;
struct in{ int l,r,id,g; }s[maxn];
bool cmp(in w,in v){ if(w.g==v.g) return w.r<v.r; return w.g<v.g ;}
int qpow(int a,int x){
int res=; while(x){
if(x&) res=(ll) res*a%Mod;
a=(ll)a*a%Mod; x>>=;
} return res;
}
void prepare()
{
f[]=rev[]=;
rep(i,,maxn-) f[i]=(ll)f[i-]*i%Mod;
rev[]=qpow(f[],Mod-);
for(int i=maxn-;i>=;i--) rev[i]=(ll)rev[i+]*(i+)%Mod;
}
int C(int n,int m){
if(n<m) return ;
return (ll)f[n]*rev[n-m]%Mod*rev[m]%Mod;
}
int main()
{
prepare(); B=sqrt();
int N; scanf("%d",&N);
rep(i,,N) scanf("%d%d",&s[i].l,&s[i].r),s[i].id=i,s[i].g=s[i].l/B;
sort(s+,s+N+,cmp);
int L=,R=,res=;
rep(i,,N){
while(L<s[i].l) res=((*res-C(L++,R))%Mod+Mod)%Mod;
while(L>s[i].l) res=(ll)(res+C(--L,R))%Mod*rev[]%Mod;
while(R<s[i].r) res=(res+C(L,++R))%Mod;
while(R>s[i].r) res=(res-C(L,R--)+Mod)%Mod;
ans[s[i].id]=res;
}
rep(i,,N) printf("%d\n",ans[i]);
return ;
}
HDU - 6333:Harvest of Apples (组合数前缀和&莫队)的更多相关文章
- HDU 6333 Harvest of Apples (分块、数论)
题目连接:Harvest of Apples 题意:给出一个n和m,求C(0,n)+C(1,n)+.....+C(m,n).(样例组数为1e5) 题解:首先先把阶乘和逆元预处理出来,这样就可O(1)将 ...
- HDU - 6333 Harvest of Apples
题意: T次询问,每次给出n,m.求sigma(k:0->m)C(n, k). 题解: 用离线莫队来做. 令S(n,m) = sigma(k:0->m)C(n, k). S(n+1, m) ...
- 【HDU 5145】 NPY and girls(组合+莫队)
pid=5145">[HDU 5145] NPY and girls(组合+莫队) NPY and girls Time Limit: 8000/4000 MS (Java/Other ...
- 2018.08.12 bzoj5301: [Cqoi2018]异或序列(前缀和+莫队)
传送门 简单的异或前缀和处理+莫队统计答案. 惊奇的发现无论开不开long long都能跑过... 代码: #include<bits/stdc++.h> #define N 100005 ...
- Harvest of Apples (HDU多校第四场 B) (HDU 6333 ) 莫队 + 组合数 + 逆元
题意大致是有n个苹果,问你最多拿走m个苹果有多少种拿法.题目非常简单,就是求C(n,0)+...+C(n,m)的组合数的和,但是询问足足有1e5个,然后n,m都是1e5的范围,直接暴力的话肯定时间炸到 ...
- HDU - 6333 Problem B. Harvest of Apples (莫队)
There are nn apples on a tree, numbered from 11 to nn. Count the number of ways to pick at most mm a ...
- 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 题意很好懂,就是组合数求和. 官方题解: 我来叨叨 ...
- 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数值范围太 ...
- HDU 6333 莫队+组合数
Problem B. Harvest of Apples Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K ...
随机推荐
- Linux 进程管理 vmstat、top、pstree命令
vmstat命令:监控系统资源 vmstat 是 Linux 中的一个综合性能分析工具,可以用来监控 CPU 使用.进程状态.内存使用.虚拟内存使用.磁盘输入/输出状态等信息.vmstat 命令格式如 ...
- Java基础_基本语法
Java基本语法 一:关键字 在Java中有特殊含义的单词(50). 二:标志符 类名,函数名,变量名的名字的统称. 命名规则: 可以是字母,数字,下划线,$. 不能以数字开头. 见名之意. 驼峰规则 ...
- JSONP跨站访问
js中几种实用的跨域方法原理详解 这里说的js跨域是指通过js在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(iframe)的数据.只要协 ...
- socket IPC(本地套接字 domain)
1.简介 socket API原本是为网络通讯设计的,但后来在socket的框架上发展出一种IPC机制,就是UNIX Domain Socket.虽然网络socket也可用于同一台主机的进程间通讯(通 ...
- windchill系统——开发_菜单栏添加选项(模型添加action)
目录:C:\ptc\Windchill_11.0\Windchill\codebase\config\actions 文件:custom-actionModels.xml和custom-actions ...
- Codeforces Round #363 (Div. 2) A、B、C
A. Launch of Collider time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- U盘安装OS
1. 老毛桃 2. 大白菜 3.
- HIVE- SCD缓慢变化
SCD缓慢变化维,比如一个用户维表,用户属性会变化,但是不会变化很剧烈,可能一年只会变化一两次,也不会所有用户的属性都会有变化,只有少量的数据发生变化,所以叫缓慢变化维.这种问题就是由于维度的变化所造 ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- Entity Framework 6 Code First 系列:无需修改实体和配置-在MySql中使用和SqlServer一致的并发控制
无需修改实体和配置,在MySql中使用和SqlServer一致的并发控制.修改RowVersion类型不可取,修改为Timestamp更不可行.Sql Server的RowVersion生成一串唯一的 ...