Problem B. Harvest of Apples

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 3088    Accepted Submission(s): 1201

Problem Description
There are n apples on a tree, numbered from 1 to n.
Count the number of ways to pick at most m apples.
 
Input
The first line of the input contains an integer T (1≤T≤105) denoting the number of test cases.
Each test case consists of one line with two integers n,m (1≤m≤n≤105).
 
Output
For each test case, print an integer representing the number of ways modulo 109+7.
 
Sample Input
2
5 2
1000 500
 
Sample Output
16
924129523
 
Source
 
Recommend
chendu   |   We have carefully selected several similar problems for you:  6361 6360 6359 6358 6357 
 
题意:有n个苹果,从中取出不超过m个苹果的方法数
分析:因为题目中的T最大是10^5,如果我们每输入一次就运算一次时间复杂度很高会超时,所以我们首先将输入离线存入ask数组
   如果直接暴力求ask中每个询问的值离线就没有什么用了,我们在这里通过对ask中的询问进行排序做到线性求解
   那么怎么排序呢?
   考虑通过杨辉三角形我们可以得到的结论:S(n,m) = S(n,m-1)+C(n,m), S(n,m) = 2*S(n-1,m) - C(n-1,m)
   通过这个式子我们知道要想计算出S(n,m)得先计算出S(n,m-1)或S(n-1,m),也就是我们要在计算n时的结果时先得出关于n-1和m-1的结果
  因此我们想到了先按n排序再按m排序,因为n取值很大,所以我们考虑将n分块,分块后每个区间的S(n,m)我们可以通过累加C(n,i)(0<=i<=m)得到,在求下个区间时通过 S(n,m) = 2*S(n-1,m) - C(n-1,m)求
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e5+10;
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll block, a[maxn], b[maxn];
struct node {
ll le, ri, id, ans;
};
node ask[maxn];
bool cmp( node p, node q ) {
if( (p.le-1)/block == (q.le-1)/block ) { //分成block大小的几块,看n属于哪一块,每一块里面按m排序
return p.ri < q.ri;
} else {
return p.le < q.le;
}
}
ll qow( ll a, ll b ) { //快速幂用于求逆元
ll ans = 1;
while(b) {
if( b&1 ) {
ans = ans*a%mod;
}
a = a*a%mod;
b /= 2;
}
return ans;
}
void init() {
a[1] = 1;
for( ll i = 2; i < maxn; i ++ ) { //计算i的阶乘
a[i] = a[i-1]*i%mod;
}
for( ll i = 1; i < maxn; i ++ ) { //计算i的阶乘的逆元
b[i] = qow(a[i],mod-2);
}
}
ll C( ll n, ll m ) { //计算组合数C(n,m)
if( n < 0 || m < 0 || m > n ) {
return 0;
}
if( m == 0 || m == n ) {
return 1;
}
return (a[n]*b[n-m]%mod)*b[m]%mod;
}
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
init();
ll T, sum = 1;
block = sqrt(maxn); //以sqrt(maxn)大小分成几块
scanf("%lld",&T);
for( ll i = 1; i <= T; i ++ ) { //离线查询
scanf("%lld%lld",&ask[i].le,&ask[i].ri);
ask[i].id = i;
}
sort(ask+1,ask+T+1,cmp); //按照cmp排序从小到大可以线性计算结果,节省时间
for( ll i = 1, le = 1, ri = 0; i <= T; i ++ ) {
while( le < ask[i].le ) { //S(n,m)=2S(n-1,m)-C(n-1,m)
sum = (2*sum-C(le++,ri)+mod)%mod;
}
while( le > ask[i].le ) { //S(n,m)=(S(n+1,m)+C(n,m))/2
sum = ((sum+C(--le,ri))*b[2])%mod;
}
while( ri < ask[i].ri ) { //S(n,m)=S(n,m-1)+C(n,m)
sum = (sum+C(le,++ri))%mod;
}
while( ri > ask[i].ri ) { //S(n,m)=S(n,m+1)-C(n,m)
sum = (sum-C(le,ri--)+mod)%mod;
}
ask[ask[i].id].ans = sum; //按标号顺序存放结果,只利用了ans,这样不会对后面有的计算造成影响
}
for( ll i = 1; i <= T; i ++ ) {
printf("%lld\n",ask[i].ans);
}
return 0;
}

  

hdu6333 Harvest of Apples 离线+分块+组合数学(求组合数模板)的更多相关文章

  1. HDU - 6333 Problem B. Harvest of Apples (莫队+组合数学)

    题意:计算C(n,0)到C(n,m)的和,T(T<=1e5)组数据. 分析:预处理出阶乘和其逆元.但如果每次O(m)累加,那么会超时. 定义 S(n, m) = sigma(C(n,m)).有公 ...

  2. HDU6333 Harvest of Apples (杭电多校4B)

    这莫队太强啦 先推公式S(n,m)表示从C(n, 0) 到 C(n, m)的总和 1.S(n, m)   = S(n, m-1) + C(n, m) 这个直接可以转移得到 2.S(n, m)   = ...

  3. hdu6333 Problem B. Harvest of Apples(组合数+莫队)

    hdu6333 Problem B. Harvest of Apples 题目传送门 题意: 求(0,n)~(m,n)组合数之和 题解: C(n,m)=C(n-1,m-1)+C(n-1,m)    设 ...

  4. HDU 6333 Harvest of Apples (分块、数论)

    题目连接:Harvest of Apples 题意:给出一个n和m,求C(0,n)+C(1,n)+.....+C(m,n).(样例组数为1e5) 题解:首先先把阶乘和逆元预处理出来,这样就可O(1)将 ...

  5. cf666 C. Codeword 组合数学 离线分块思想

                      time limit per test 6 seconds memory limit per test 256 megabytes input standard i ...

  6. HDU-6333 Problem B. Harvest of Apples 莫队

    HDU-6333 题意: 有n个不同的苹果,你最多可以拿m个,问有多少种取法,多组数据,组数和n,m都是1e5,所以打表也打不了. 思路: 这道题要用到组合数的性质,记S(n,m)为从n中最多取m个的 ...

  7. 2018年多校第四场第二题 B. Harvest of Apples hdu6333

    题意:给定10^5以内的n,m求∑组合数(n,i),共10^5组数据. 题解: 定义 S(n, m) = \sum_{i = 0} ^ {m} {n \choose i}S(n,m)=∑​i=0​m​ ...

  8. Harvest of Apples

    问题 B: Harvest of Apples 时间限制: 1 Sec  内存限制: 128 MB提交: 18  解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 Ther ...

  9. HDU6333 求组合数前m项的和

    目录 分块 莫队 @ HDU6333:传送门 题意:求组合数前m项的和. 在线分块or离线莫队 分块 重要的一个定理: \[C_{n}^{m} = 0\;\;m > n\] \[C_{n}^{m ...

随机推荐

  1. Python—三目运算

    Python 可通过 if 语句来实现三目运算的功能,因此可以近似地把这种 if 语句当成三目运算符.作为三目运算符的 if 语句的语法格式如下: (True_statements) if (expr ...

  2. PID算法资料【视频+PDF介绍】

    最近一直有网友看到我的博客后,加我好友,问我能不能给发一些PID的资料,今天找了一些资料放到百度网盘上,给大家下载: 视频资料 链接:https://pan.baidu.com/s/12_IlLgBI ...

  3. Apache ActiveMQ任意文件写入漏洞(CVE-2016-3088)复现

    Apache ActiveMQ任意文件写入漏洞(CVE-2016-3088)复现 一.漏洞描述 该漏洞出现在fileserver应用中,漏洞原理:ActiveMQ中的fileserver服务允许用户通 ...

  4. Usaco Training [2.1] The Castle 搜索

    传送门 题目的输出的4个信息 前两个很容易,dfs,bfs都可以,图怎么建都可以 后两个在搜索的时候记录belong[i][j]和已有的size即可 代码应该比不少题解清晰吧 #include < ...

  5. Linux服务部署Yapi项目(安装Node Mongdb Git Nginx等)

    Linux服务部署Yapi 一,介绍与需求 1,我的安装环境:CentOS7+Node10.13.0+MongoDB4.0.10. 2,首先安装wget,用于下载node等其他工具 yum insta ...

  6. python编写环境(种类)

    python编写环境(种类) 官方推荐 cpython 转成C的字节码 jython转成Java的字节码 ironpython转成C#字节码 pypy转换成动态编译 开发快,运行快

  7. Hbase多版本(version)数据写入和读取

    1. 首先创建一个支持多版本的hbase表 create }   2.put几条测试数据 put ','f1:name','jack1' put ','f1:name','jack2' 3.读取多版本 ...

  8. Oracle cursor学习笔记

    目录 一.oracle库缓存 1.1.库缓存简介 1.2.相关概念 1.3.库缓存结构 1.4.sql执行过程简介 二.oracle cursor 2.1.cursor分类 2.2.shared cu ...

  9. jQuery Validate 可选项

  10. ElasticSearch:常用的基础查询与过滤器

    match_all(获取所有索引文档) quert_string(获取包含指定关键字文档) 默认查询_all字段,_all字段是由所有字段组合而成的,可以通过description:关键字,获取通过请 ...